Skip to content Skip to sidebar Skip to footer

How To Make Multi Id Array Object To All In One

I working with some wordpress project this project about searching tour by terms category and this i use jQuery ui catcomplete by my array is not support by catcomplete so i want t

Solution 1:

You can use map, reduce and concat

Use map to convert objects into 2 dimentional array.

Use reduce to loop thru the array and merge it using concat

let data = {
  1: {
    1: {
      label: "Namibia",
      category: "Afrika"
    },
    2: {
      label: "Sydafrika",
      category: "Afrika"
    },
    3: {
      label: "Tanzania",
      category: "Afrika"
    },
    4: {
      label: "Madagaskar",
      category: "Afrika"
    },
  },
  2: {
    1: {
      label: "Colombia",
      category: "Amerika"
    },
    2: {
      label: "Kuba",
      category: "Amerika"
    },
    3: {
      label: "Private: Peru",
      category: "Amerika"
    },
    4: {
      label: "Panama",
      category: "Amerika"
    },
    5: {
      label: "Costa Rica",
      category: "Amerika"
    },
  },
  3: {
    1: {
      label: "Private: Södra Indien",
      category: "Asien"
    },
    2: {
      label: "Indonesien",
      category: "Asien"
    },
    3: {
      label: "Filippinerna",
      category: "Asien"
    },
    4: {
      label: "Indien",
      category: "Asien"
    },
    5: {
      label: "Kambodja",
      category: "Asien"
    },
    6: {
      label: "Vietnam",
      category: "Asien"
    },
    7: {
      label: "Myanmar",
      category: "Asien"
    },
    8: {
      label: "Sri Lanka",
      category: "Asien"
    },
    9: {
      label: "Thailand",
      category: "Asien"
    },

  }
};

let newData = Object.values(data).map(v => {
  let x = [];
  for (let k in v) x.push(v[k]);
  return x;
}).reduce((c, v) => {
  c = c.concat(v);
  return c;
}, []);

console.log(newData);

You can also try a shorter version:

let newData = Object.values(data).reduce((c, v) => c.concat(Object.values(v).map(i => i)),[]);

Post a Comment for "How To Make Multi Id Array Object To All In One"