Get an Array of Property Values from an Object 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().

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)

Accessing Object Property Values Within an Array - JavaScript

You can acces items in arrays at given position by their index. In javascript indexes of arrays are starting with 0: myArray[0]. To access the property of the returned object just use dot-notation: myArray[0].myProperty.





let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];


console.log(object1[1].job);

How to check if property of an objects of array matches with one of the values in another array of object

First off, you'll have to make sure if the ID types are correct. Users has Number type for IDs but attendingUsers has String type.

Let's say they're both the same type for the code below (I'm going with string).

You can turn the attendingUsers array into an array of strings with:

const attendingUsersIds = attendingUsers.map(attendingUser => attendingUser.id)

Then match the ids with:

const matchedUsers = users.filter(user => attendingUsersIds.includes(user.id))

If they're intended to not be the same type, you can use user.id.toString() to turn the Number into a String or parseInt(attendingUser.id) to turn the String into a Number.

How to retrieve the array of objects having same property value in javascript

With reduce it's more cumbersome to find out all duplicates.
You can use closure.





const list = [
{ id: 1, name: "dev", items: "sales", code: "IN" },
{ id: 2, name: "lena", items: "finance", code: "SG" },
{ id: 3, name: "lisa", items: "sales", code: "AU" },
{ id: 4, name: "mano", items: "marketing", code: "IN" },
{ id: 5, name: "lisa", items: "gul gul", code: "AU" },
{ id: 6, name: "anthony", items: "some", code: "AU" },
{ id: 7, name: "mark", items: "gul gul", code: "AU" }
];

const findByItems = (eq) => (arr) => arr.filter(
(x, i) => arr.find((y, j) => i !== j && eq(x, y))
)

const duplicatedItems = findByItems((a, b) => a.items === b.items);


console.log(duplicatedItems(list))

Get an array with one property from an array of objects in ES6

Use the .map() method to loop through the items.

Simply put:

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

- MDN web docs

The method allows to call the provided callback function once for each element of the array. It creates a new array made of the mapped elements.
You can pass both the element and index as arguments of the callback. The latter is optional (when you don't need it in the callback).

It's a clean way of accessing objects in JavaScript. Here is the code:





let array = [
{foo: 1, bar: 2},
{foo: 3, bar: 4}
]

console.log(array.map( e => e.foo ))

Get an array of property values from an object array

You can use the map method, which transform an array of a certain type to an array of another type - in your case, from array of Employee to array of Int:

var array = [Employee]()
array.append(Employee(id: 4, firstName: "", lastName: ""))
array.append(Employee(id: 2, firstName: "", lastName: ""))

let ids = array.map { $0.id }

How can I take only few property from my array objects in js

You can use Array.prototype.map to filter out the required properties.





const array = [{_id: '621723ddc1f73de5f7e4dcb9', label: 'new 22', slug: 'new-22', vendor: 'admin', options: Array(1)}, {_id: '6217272ec1f73de5f7e4dcba', label: 'new 33', slug: 'new-33', vendor: 'admin', options: Array(1)}];

const newArray = array.map(({label, slug}) => ({label, slug}));

console.log(newArray);

How to get list with of property from array of objects unless it contains another item with certain value?

1) You can filter the elements with condition item.isPresent === true and then map over it to get the final result as:

employees
.filter((item) => item.isPresent === true)
.map((o) => o.id);

or you can also do as:

employees.filter((item) => item.isPresent).map((o) => o.id)





const employees = [{
n: 'case 1',
date: '2021-05-4',
id: '123',
user: [{
name: 'Vlad',
id: '1'
}, {
name: 'Misha',
id: '2'
}],
isPresent: true,
},
{
caseName: 'case 2',
date: '2021-05-4',
id: '124',
user: [{
name: 'Alina',
id: '3'
}, {
name: 'Alex',
id: '4'
}],
isPresent: true,
},
{
caseName: 'case 3',
date: '2021-05-4',
id: '126',
user: [],
isPresent: false,
},
]

const result = employees
.filter((item) => item.isPresent === true)
.map((o) => o.id);
console.log(result);


Related Topics



Leave a reply



Submit