2

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: { ... }
}
3
  • Object.keys(obj).filter(r => ['mango','apple'].indexOf(r) > -1).map(i => obj[i]); should be enough. Commented Apr 27, 2018 at 6:46
  • @Pranav, it's working for you ? Commented Apr 27, 2018 at 11:30
  • @briosheje it is not working. It returns array of values of mango and apple Commented Apr 30, 2018 at 3:55

6 Answers 6

2

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']));

Sign up to request clarification or add additional context in comments.

1 Comment

That's clever, indeed.
0

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

Comments

0

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);

Comments

0

You can filter data of specific keys like this.

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

Comments

0

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));

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.