【问题标题】:Converting Json Object array Values to a single array将 Json 对象数组值转换为单个数组
【发布时间】:2019-02-01 09:27:48
【问题描述】:

美好的一天

我正在尝试使用自动完成 jquery 框架.. 购买我的问题是我如何从 json 文件或像下面这样的快速键入的 json 对象获取我的对象并获取所有 Object.values(arry) 到像下面这样的单个数组..

[“samue”、“anthon”、“Harmony”、“Johnson”]

并且必须是一个数组.. 因为自动完成框架只适用于数组

const myobj = [
    {
      "name": "samuel",
      "surname": "anthony"
    },
    {
      "name": "Harmony",
      "surname": "Johnson"
    }
  ]

  const file = "json/user.json";

  fetch('file')
  .then(res => res.json())
  .then((data) =>{
      let i = 0;
      const val = Object.values(data[i]);
      if(i < data.length){
        i++;
        const all = Object.values(data[i]);
        console.log(all);
      }

      var input = document.getElementById("countries");
      var awesomplete = new Awesomplete(input, {
          minChars: 1,
          maxItems: 5,
          autoFirst: true
      });
      awesomplete.list = val;

      //wanted array of
      //["samuel", "anthon", "school"]
  })

【问题讨论】:

    标签: javascript arrays function autocomplete


    【解决方案1】:

    使用reduce

    const myobj = [
        {
          "name": "samuel",
          "surname": "anthony"
        },
        {
          "name": "Harmony",
          "surname": "Johnson"
        }
      ]
      console.log(myobj.reduce((acc,e)=>{acc.push(e.name);acc.push(e.surname);return acc},[]))

    使用forEach

    const myobj = [
        {
          "name": "samuel",
          "surname": "anthony"
        },
        {
          "name": "Harmony",
          "surname": "Johnson"
        }
      ]
    
    var a=[];
    myobj.forEach((e)=>{a.push(e.name);a.push(e.surname)})
    console.log(a)

    【讨论】:

      【解决方案2】:

      要将myObj 转换为您想要的数组,您可以使用.flatMapObject.values,如下所示:

      const myobj = [{
          "name": "samuel",
          "surname": "anthony"
        },
        {
          "name": "Harmony",
          "surname": "Johnson"
        }
      ];
      
      const res = myobj.flatMap(Object.values);
      console.log(res);

      但是,请注意。 flatMap 并非在所有浏览器中都可用,因此它没有最好的浏览器兼容性。

      如果您不能使用.flatMap,您可以使用.reducedestructing assignment 作为替代:

      const myobj = [{
          "name": "samuel",
          "surname": "anthony"
        },
        {
          "name": "Harmony",
          "surname": "Johnson"
        }
      ];
      
      const res = myobj.reduce((acc, {name, surname}) => [...acc, name, surname], []);
      console.log(res);

      【讨论】:

        【解决方案3】:

        您可以使用... 扩展运算符将push() Object.values(obj) 转换为期望数组

        const myobj = [
            {
              "name": "samuel",
              "surname": "anthony"
            },
            {
              "name": "Harmony",
              "surname": "Johnson"
            }
          ]
        let arr = []
        myobj.forEach(obj=> arr.push(...Object.values(obj)));
        console.log(arr);

        【讨论】:

          【解决方案4】:

          您可以使用Array.map() method

          一个示例用例可能是:

          const oldArr = [
              {
                "name": "Samuel",
                "surname": "anthony"
              },
              {
                "name": "Harmony",
                "surname": "Johnson"
              }
           ]
          
          const newArr = oldArr.map(x => x.name) // ['Samuel', 'Harmony']
          

          【讨论】:

            【解决方案5】:

            扁平化对象:

            const myobj = [{
            "name": "samuel",
            "surname": "anthony"
              },
              {
            "name": "Harmony",
            "surname": "Johnson"
              }
            ];
            
            var flatten=myobj.reduce((arr, v)=> [...arr,...[v.name,v.surname]],[]);
            console.log(flatten);

            【讨论】:

              猜你喜欢
              • 2013-08-28
              • 1970-01-01
              • 2016-09-19
              • 1970-01-01
              • 1970-01-01
              • 2020-12-14
              • 2019-07-29
              • 1970-01-01
              • 2020-10-04
              相关资源
              最近更新 更多