0

I am trying to get some particular property with value from an array object.But it is not working.How to resolve this issue?

var arr1 = [
  {
   name: 'john',
   team: [12, 23, 34],
   sid: 1023,
   options:{boolean:true}
  },
  {
    name: 'john',
    team: [152, 233, 354],
    sid: 1024,
    options:{boolean:true}
   },
  {
   name: 'john',
   team: [152, 273, 314],
   sid: 1025,
   options:{boolean:true}
  },
  ];


 const filteredResult = arr1.find((e) => e.!sid && e.!options);

 console.log(filteredResult);

filteredResult should be like

[
  {
    name: 'john',
    team: [12, 23, 34]
    
  },
  {
    name: 'john',
    team: [152, 233, 354]
  },
  {
    name: 'john',
    team: [152, 273, 314]
  }
];

Demo: https://stackblitz.com/edit/js-73yhgm?file=index.js

EMahan K
  • 417
  • 1
  • 19

5 Answers5

2

array.map() should do it. Just return the specific properties that you want

var arr1 = [{
    name: 'john',
    team: [12, 23, 34],
    sid: 1023,
    options: {
      boolean: true
    }
  },
  {
    name: 'john',
    team: [152, 233, 354],
    sid: 1024,
    options: {
      boolean: true
    }
  },
  {
    name: 'john',
    team: [152, 273, 314],
    sid: 1025,
    options: {
      boolean: true
    }
  },
];

const result = arr1.map(x => ({
  name: x.name,
  team: x.team
}))

console.log(result)
Chris G
  • 1,598
  • 1
  • 6
  • 18
0

You can destructure only the properties you want to be returned.

const arr1=[{name:"john",team:[12,23,34],sid:1023,options:{boolean:!0}},{name:"john",team:[152,233,354],sid:1024,options:{boolean:!0}},{name:"john",team:[152,273,314],sid:1025,options:{boolean:!0}},];
const res = arr1.map(({name, team}) => ({name, team}));
console.log(res);

Or, with a dynamic array of properties to extract:

const arr1=[{name:"john",team:[12,23,34],sid:1023,options:{boolean:!0}},{name:"john",team:[152,233,354],sid:1024,options:{boolean:!0}},{name:"john",team:[152,273,314],sid:1025,options:{boolean:!0}},];
const keys = ['name', 'team'];
const res = arr1.map(o => Object.fromEntries(keys.map(k => [k, o[k]])));
console.log(res);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

arr1.find() will only return the first matching element. Use array filter instead if you want to get all the matching elements.

bestuh
  • 11
  • 1
0

You can build a function that will filter down to the property set you want to return. Then use the map function on the array and pass in the custom function (selectFewerProps) just defined.

var arr1 = [
  {
    name: 'john',
    team: [12, 23, 34],
    sid: 1023,
    options:{boolean:true}
  },
  {
    name: 'john',
    team: [152, 233, 354],
    sid: 1024,
    options:{boolean:true}
  },
  {
    name: 'john',
    team: [152, 273, 314],
    sid: 1025,
    options:{boolean:true}
  },
];

function selectFewerProps(show){
  const {name, team} = show;
  return {name, team};
}

const filteredResult = arr1.map(selectFewerProps);

console.log(filteredResult);
M. Rizzo
  • 1,611
  • 12
  • 24
0

You can also use array.forEach() like that :

    let result = [];

    var arr1 = [
      {
        name: 'john',
        team: [12, 23, 34],
        sid: 1023,
        options: {
            boolean: true
        }
      },
      {
        name: 'john',
        team: [152, 233, 354],
        sid: 1024,
        options: {
            boolean: true
        }
      },
      {
        name: 'john',
        team: [152, 273, 314],
        sid: 1025,
        options: {
            boolean: true
        }
      },
    ];

    arr1.forEach(obj => {
        result.push({
            name: obj.name,
            team: obj.team
        });
    });

console.log(result);
tnougosso
  • 72
  • 1
  • 4