简体   繁体   English

将 Json 对象数组值转换为单个数组

[英]Converting Json Object array Values to a single array

Good day再会

I am trying to use an autocomplete jquery framework.. buy my problem is how can i get my objects either from a json file or just a quick typed json object like the below and get all the Object.values(arry) to a single array like below..我正在尝试使用自动完成 jquery 框架.. 购买我的问题是如何从 json 文件或仅像下面这样的快速键入的 json 对象获取我的对象并将所有 Object.values(arry) 获取到单个数组像下面..

["samue", "anthon", "Harmony", "Johnson"] ["samue", "anthon", "Harmony", "Johnson"]

and must be an array.. because auto complete frameworks only works with arrays并且必须是一个数组..因为自动完成框架只适用于数组

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"]
  })

To convert myObj to your desired array you can use .flatMap and Object.values like so:要将myObj转换为您想要的数组,您可以使用.flatMapObject.values如下所示:

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

However, do note.但是,请注意。 flatMap is not available in all browsers and so it doesn't have the best browser compatibility. flatMap并非在所有浏览器中都可用,因此它没有最好的浏览器兼容性。

If you cannot use .flatMap you can use .reduce and destructing assignment as an alternative:如果您无法使用.flatMap可以使用.reduce破坏任务作为一种替代方案:

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

Using reduce使用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},[]))

Using forEach使用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)

You can push() Object.values(obj) into desire array using ... spread operator您可以使用...展开运算符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);

You could use the Array.map() method您可以使用Array.map() 方法

An example use case might be:一个示例用例可能是:

const oldArr = [
    {
      "name": "Samuel",
      "surname": "anthony"
    },
    {
      "name": "Harmony",
      "surname": "Johnson"
    }
 ]

const newArr = oldArr.map(x => x.name) // ['Samuel', 'Harmony']

The Flatten Object:展平对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM