1

I have an array of objects in Javascript:

[
{a: 1, b: 2, c: 4},
{d: 10, a: 9, r: 2},
{c: 5, e: 2, j: 41}
]

I would like to return an array of all the objects that have the key name c by using the key name for the lookup.

What is the best way to accomplish this?

7 Answers7

2

You can use filter method and hasOwnProperty

const arr = [{a: 1, b: 2, c: 4},{d: 10, a: 9, r: 2},{c: 5, e: 2, j: 41}];
const myArray = array.filter(e => e.hasOwnProperty('c'))
vitaretnama
  • 126
  • 3
1

.filter + in operator

var arr = [
    {a: 1, b: 2, c: 4},
    {d: 10, a: 9, r: 2},
    {c: 5, e: 2, j: 41}
];
var result = arr.filter((x) => 'c' in x);
console.log(result)
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Jay Patel
  • 231
  • 1
  • 4
0

use filter(), and maybe hasOwnProperty() or another way to check if the property is present on the object.

const myarray = [
    { a: 1, b: 2, c: 4 },
    { d: 10, a: 9, r: 2 },
    { c: 5, e: 2, j: 41 }
];

const processedarray = myarray.filter( item => item.hasOwnProperty( 'c' ) );

console.log( processedarray );
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
0

Try using filter and hasOwnProperty.

const data = [{
    a: 1, b: 2, c: 4
  },
  {
    d: 10, a: 9, r: 2
  },
  {
    c: 5,  e: 2, j: 41
  }
];

const filtered = (arr, prop) =>
  arr.filter(a => a.hasOwnProperty(prop));
  
console.log(
  JSON.stringify(filtered(data, 'c'))
);

If you want to return an array of objects with multiple specific keys, you can combine it with some.

const data = [{
    a: 1, b: 2, c: 4
  },
  {
    d: 10, a: 9, r: 2
  },
  {
    c: 5,  e: 2, j: 41
  }
];
  
const filtered = (arr, ...props) =>
  arr.filter(a => props.some(prop => a.hasOwnProperty(prop)));

console.log(
  JSON.stringify(filtered(data, 'b', 'j'))
);
Ian
  • 1,198
  • 1
  • 5
  • 15
0

the easiest way is to loop through the array to check if the object has a key named c

var arr = [
    { a: 1, b: 2, c: 4 },
    { d: 10, a: 9, r: 2 },
    { c: 5, e: 2, j: 41 }
]

// they both do the same job
var result = arr.filter(item => Object.keys(item).some(key => key === 'c'))
var filtered = arr.filter(item => item.hasOwnProperty('c'))


console.log(result);
console.log(filtered);
Rawand
  • 377
  • 3
  • 11
  • `.some(key => key.indexOf('c') > -1)` will be true for keys *containing* the letter 'c'. I think you want `.some(key => key === 'c')` – danh Aug 02 '22 at 02:05
  • you are right, but i guess he got the idea, I will change the answer @danh – Rawand Aug 02 '22 at 14:23
0

You can simply achieve this by using Array.filter() method along with Object.hasOwn().

Live Demo :

const arr = [
{a: 1, b: 2, c: 4},
{d: 10, a: 9, r: 2},
{c: 5, e: 2, j: 41}
];

const res = arr.filter(obj => Object.hasOwn(obj, 'c'));

console.log(res);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
-1

This is My answer. Thank you

const arr = [
  {a: 1, b: 2, c: 4},
  {d: 10, a: 9, r: 2},
  {c: 5, e: 2, j: 41}
]

const filter = (arr, byKey) => {
  return arr.filter((ob) => {
    return Object.keys(ob).includes(byKey)
  })
}

console.log(filter(arr, 'c'))