0

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

5 Answers 5

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.