Home > Net >  How can I take only few property from my array objects in js
How can I take only few property from my array objects in js

Time:02-24

I am trying to get only 2 properties from array objects.

This is my array:

 [
0: {_id: '621723ddc1f73de5f7e4dcb9', label: 'new 22', slug: 'new-22', vendor: 'admin', options: Array(1)}
1: {_id: '6217272ec1f73de5f7e4dcba', label: 'new 33', slug: 'new-33', vendor: 'admin', options: Array(1)}
]

I am trying to get only label and slug my expectation is :

[
0: {label: 'new 22', slug: 'new-22'}
1: {label: 'new 33', slug: 'new-33'}
]

I have tried like this way: but it's returning full array objects

          let tempArray;
          for (let i = 0; i < data.length; i  = 2) {
                tempArray = data.slice(data[i], data[i   2]);
              }
                setAttributeLabel(tempArray);

CodePudding user response:

You can use Array.prototype.map to filter out the required properties.

const array = [{_id: '621723ddc1f73de5f7e4dcb9', label: 'new 22', slug: 'new-22', vendor: 'admin', options: Array(1)}, {_id: '6217272ec1f73de5f7e4dcba', label: 'new 33', slug: 'new-33', vendor: 'admin', options: Array(1)}];

const newArray = array.map(({label, slug}) => ({label, slug}));

console.log(newArray);

CodePudding user response:

Just iterate of the array and create a new object of required properties and push in temp array

const data = [{
    _id: '621723ddc1f73de5f7e4dcb9',
    label: 'new 22',
    slug: 'new-22',
    vendor: 'admin'
  },
  {
    _id: '6217272ec1f73de5f7e4dcba',
    label: 'new 33',
    slug: 'new-33',
    vendor: 'admin'
  }
]

let tempArray = [];
for (let i = 0; i < data.length; i  ) {
  tempArray.push({
    label: data[i].label,
    slug: data[i].slug

  })
}
console.log(tempArray);

CodePudding user response:

You can user Array.map to create a new array. For each element mapped, you create a new object containing only the properties you need:

arr.map(a => { return { label: a.label, slug: a.slug } } );

CodePudding user response:

Instead of using loops, try to use javascript pre-defined functions like maps.

    array.map(({ label, slug }) => { 
     return {label, slug}
    });

CodePudding user response:

You can use Array.prototype.reduce to loop through the data set and return only the value which you need as part of a new object

let data = [{_id: '621723ddc1f73de5f7e4dcb9', label: 'new 22', slug: 'new-22', vendor: 'admin', options: Array(1)},{_id: '6217272ec1f73de5f7e4dcba', label: 'new 33', slug: 'new-33', vendor: 'admin', options: Array(1)}
];

const result = data.reduce((accumulator, current) => {
  return accumulator.concat({label: current.label, slug: current.slug});
}, []);

console.log(result);

CodePudding user response:

Use Array.map() to iterate the input array and get the required properties from an object.

// Input array
const arr = [
{_id: '621723ddc1f73de5f7e4dcb9', label: 'new 22', slug: 'new-22', vendor: 'admin', options: Array(1)},
{_id: '6217272ec1f73de5f7e4dcba', label: 'new 33', slug: 'new-33', vendor: 'admin', options: Array(1)}];

// Array.map() to iterate the input array and get the required properties from an object.
const res = arr.map(({label, slug}) => {
    return {label, slug}
});

// Result
console.log(res);

  • Related