Converting Json Object array Values to a single array

坚强是说给别人听的谎言 提交于 2021-02-18 19:32:39

问题


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..

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

回答1:


Using 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

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:


To convert myObj to your desired array you can use .flatMap and Object.values like so:

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.

If you cannot use .flatMap you can use .reduce and destructing assignment as an alternative:

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

const res = myobj.reduce((acc, {name, surname}) => [...acc, name, surname], []);
console.log(res);



回答3:


You can push() Object.values(obj) into desire array using ... spread operator

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



回答4:


You could use the Array.map() method

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']



回答5:


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);


来源:https://stackoverflow.com/questions/54476513/converting-json-object-array-values-to-a-single-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!