From an Array of Objects, Extract Value of a Property as Array

From an array of objects, extract value of a property as array

Here is a shorter way of achieving it:

let result = objArray.map(a => a.foo);

OR

let result = objArray.map(({ foo }) => foo)

You can also check Array.prototype.map().

From an array of objects, extract value of two properties into an array

This should work - if your elements don't have a foo or anotherfoo they'll come out as undefined

objArray = [{  foo: 1,  bar: 2,  anotherfoo: 5}, {  foo: 3,  bar: 4,  anotherfoo: 8}];
function myfunction(arr) { return arr.map(function(e) { return { foo: e.foo, anotherfoo: e.anotherfoo }; });}
newArray = myfunction(objArray);console.log(newArray);
// newArray is [{foo:1, anotherfoo:5},{foo:3, anotherfoo:8}]

Extract value from array of objects

One more variant:

var arr = [{ type: 'month', value: '9' },
{ type: 'day', value: '11' },
{ type: 'year', value: '2021' },
{ type: 'hour', value: '7' },
{ type: 'minute', value: '35' },
{ type: 'second', value: '07' }];

// var month = arr.filter(x => x.type == 'month')[0].value; // less efficient

var month = arr.find(x => x.type == 'month').value; // more efficient

console.log(month) // 9

Select a property from an array of objects based on a value : Javascript

You can use reduce and check if the checked property is true, then push (As pointed out by assoron) the value to the accumulator - there is no need for 2 loops:

const arr = [  { "value": "abc", "checked": true },  { "value": "xyz", "checked": false },  { "value": "lmn", "checked": true }]
const filtered = arr.reduce((a, o) => (o.checked && a.push(o.value), a), []) console.log(filtered)

Extracting all values from a Array of Objects based on keys value

The simplest thing to do would be to use map() to build an array of the cuisines. Then you can loop through it or build a string from it as required.

var arr = [{"matchedKey":"cuisineType","cuisineType":"Indian","group":"group"},  {"matchedKey":"cuisineType","cuisineType":"Italian","group":"group"},  {"matchedKey":"cuisineType","cuisineType":"Asian","group":"group"},  {"matchedKey":"cuisineType","cuisineType":"Japanese","group":"group"},  {"matchedKey":"cuisineType","cuisineType":"African","group":"group"}]
var cuisines = arr.map(function(el) { return el.cuisineType;});
console.log(cuisines); // arrayconsole.log(cuisines.join(', ')); // formatted string

Extract a property inside of an array of objects which is inside of another array of objects and have the result as an array of strings

Use flatMap to take the responses array, take their response strings, and combine them:

const surveyResponses = [
{createdAt: new Date(), responses: [{questionId: 'input1', response: 'Bla'}]},
{createdAt: new Date(), responses: [{questionId: 'input1', response: 'Blo'}]}
];

const result = surveyResponses.flatMap(
item => item.responses.map(
({ response }) => response
)
);
console.log(result);

Extract value from the array of objects

You can use find function.

const result = providerList.find((x) => x.id === yourId);
const name = result? result.name : null;

Or you can get it with lodash library.

const name = _.get(_.find(providerList, {id: yourId}), 'name');


Related Topics



Leave a reply



Submit