简体   繁体   中英

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

["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:

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

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)

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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