JavaScript Extract Certain Properties from All Objects in 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().

Extract certain properties from all objects in array

Use object destructuring to get the properties, and generate a new object using shorthand property names:

const dummyArray = [{ "att20": "att20", "att30": "att30", "att70": "att70", "att80": "att80"}, { "att20": "att20", "att30": "att30", "att70": "att70", "att80": "att80"}];
const result = dummyArray.map(({ att20, att30, att70, att80 }) => ({ att20, att30, att70, att80}));
console.log(result);

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)

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

How to get a subset of a javascript object's properties

Using Object Destructuring and Property Shorthand

const object = { a: 5, b: 6, c: 7  };const picked = (({ a, c }) => ({ a, c }))(object);
console.log(picked); // { a: 5, c: 7 }

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

Extracting certain properties from an object

You can at first get all the keys, then filter them by a type property value of the corresponding object for that key in the parent object. For example:

let myObject = {
badProperty1: {type: 'object'},
goodProperty1: {type: 'integer'},
badProperty2: {type: 'object'},
goodProperty2: {type: 'string'},
badProperty3: {type: 'object'},
goodProperty3: {type: 'integer'},
};

let result = Object.keys(myObject).filter(key => ['integer', 'string'].includes(myObject[key].type));

console.log(result)

Extract these properties from an array of Objects

You could map the wanted keys of the object and generate a new object by mapping the array.

function getSubset(array, keys) {    return array.map(o => Object.assign(...keys.map(k => ({ [k]: o[k] }))));}
var objArr = [{ name: "name", description: "description", date: "date" }, { name: "name", description: "description", date: "date" }, { name: "name", description: "description", date: "date" }];
console.log(getSubset(objArr, ['name', 'description']));

How to extract the property from array of objects matching the specific characters (contains query)

You can use regular expressions as well as Array methods to match the character search:

const arr = [{ ID: 3, Title: 'License 1' }, ... ];
const query = 'lic';
const reg = new RegExp('.*' + query + '.*', 'gi');

arr.filter((x) => x.ID.match(reg)).map((x) => x.ID);
// [3, 4, 36, 37, ...]

From an array of objects, extract value of properties for each object and put in a different array

According to your desired result, I think you can use ES6 functions.

const result = yourTable.map(element => Object.values(element));

Using map() function, you go through all elements, and extract from each object its values.



Related Topics



Leave a reply



Submit