PranavPinarayi
PranavPinarayi

Reputation: 3217

Filter javascript object with key

I have a javascript object like this,

{
  apple: { .... },
  orange: { ... },
  mango: { ... },
  jackfruit: { ... }
}

and I want to grenerate a new object from this object. I want values of cetain keys only, like ['mango', apple' ] Is there any filter function to filter these items from this object using this array?

My resulting object should be like

{
  mango: { ... }
  apple: { ... }
}

Upvotes: 2

Views: 117

Answers (6)

Nina Scholz
Nina Scholz

Reputation: 386883

You could spread the single objects and assign them to a new object.

function subtract(object, keys) {
    return Object.assign({}, ...keys.map(key => ({ [key]: object[key] })));
}

var fruits = { apple: 'a', orange: 'o', mango: 'm', jackfruit: 'j' };

console.log(subtract(fruits, ['mango', 'orange']));

Upvotes: 2

qxg
qxg

Reputation: 7036

If you can use the Ramda library,

R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1}

See http://ramdajs.com/docs/#pick

Upvotes: 0

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27242

Try this :

var jsObj = {
  apple: "Apple",
  orange: "Orange",
  mango: "Mango",
  jackfruit: "jackFruit"
}

var arr = ['mango', 'apple'];

var newObj = {};

arr.filter(item => newObj[item] = jsObj[item]);

console.log(newObj);

Upvotes: 0

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48437

Is there any filter function to filter these items from this object using this array?

Yes, you can use reduce method.

let dict = {
  apple: { color: 'red'},
  orange: { color: 'orange' },
  mango: { color:'green' },
  jackfruit: { color: 'yellow' }
}
let array =  ['mango', 'apple' ]

console.log(array.reduce((obj, key) => ({ ...obj, [key]: dict[key] }), {}));

Another method is using destructing feature.

let dict = {
      apple: { color: 'red'},
      orange: { color: 'orange' },
      mango: { color:'green' },
      jackfruit: { color: 'yellow' }
}
let array =  ['mango', 'apple' ]

console.log((({ mango, apple}) => ({ mango, apple }))(dict));

Upvotes: 0

Sheikh Wahaj
Sheikh Wahaj

Reputation: 141

You can filter data of specific keys like this.

var FilteredObjectsByKeys = Object.assign({}, ...['mango', 'apple'].map(x => fruits[x]));

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30729

You can use forEach loop over the array for which you want to get the value for the key in fruitsObj

var fruitsObj = {
  apple: {rate: 100},
  orange: { rate: 200 },
  mango: { rate: 300 },
  jackfruit: { rate: 400 }
}

var filterArray = ['mango', 'apple'];
var res = [];
filterArray.forEach((fruit)=> res.push(fruitsObj[fruit]));
console.log(res);

If you want to use filter and map, you can filter out the keys first and then use map to get the value for that keys.

var fruitsObj = {
  apple: {rate: 100},
  orange: { rate: 200 },
  mango: { rate: 300 },
  jackfruit: { rate: 400 }
}

var filterArray = ['mango', 'apple'];
var res = Object.keys(fruitsObj).filter(fruitKey => ['mango','apple'].indexOf(fruitKey) !== -1).map(filterKey => fruitsObj[filterKey]);
console.log(res);

Upvotes: 0

Related Questions