javascript filter array of objects by index
let newArray = array.filter(function(item) {
return condition;
});// Our data
const fruits = {
apple: {
qty: 300,
color: "green",
name: "apple",
price: 2
},
banana: {
qty: 130,
color: "yellow",
name: "banana",
price: 3
},
orange: {
qty: 120,
color: "orange",
name: "orange",
price: 1.5
},
melon: {
qty: 70,
color: "yellow",
name: "melon",
price: 5
}
};
// Now let"s create a map function
const map = (obj, fun) =>
Object.entries(obj).reduce(
(prev, [key, value]) => ({
...prev,
[key]: fun(key, value)
}),
{}
);
// Finally let's map by color for example
const myFruits = map(fruits, (_, fruit) => fruit.color);
/*
{ apple: 'green',
banana: 'yellow',
orange: 'orange',
melon: 'yellow' }
/*
Also in JavaScript:
- li dots
- Material-ui Accessibility icon
- arithmetic expressions in scheme
- remove duplicates in json in flutter
- prevent adding the item twice in an array javascript
- how to get seconds in timstamps js
- pass element from child to parent react
- funcion dentro de objeto
- indexof javascript duplicate arrays
- javascript algorithms and data structures interview questions
- jquery class list
- react map example leaflets
- trigger click jquery
- how to validate date in react
- how to replace first and last character of string in javascript
- random number generator js
- javascript get first 2 char
- creating react app using npx
- javascript change select options dynamically
- javascript extend object
- what is payload in redux
- null undefined javascript
- nodemon.json env
- javascript clear form after dubmit