Javascript - create a singleton array out of object array [duplicate] - javascript

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 6 years ago.
I have an array of objects:
[{
name: "test",
age: 20,
gender: "male"
},
{
name: "test2",
age: 22,
gender: "female"
}]
Frequently I need to create a singleton array which contains a specific property from the object array above, for example extract only the names from the array above and create an array from it:
NewArray = ["test","test2"]
Currently I loop over the object array and push the property I need to the new array.
Is there a quick way to do it in Javascript/ES instead of looping every time I need to get specific property?

var people = [{
name:'test1',
age:20
}, {
name:'test2',
age:30
}]
let names = people.map(function(item) {
return item.name
});
console.log(names);

Related

JavaScript: convert an array of string to an array of objects [duplicate]

This question already has answers here:
Convert array of strings into an array of objects
(6 answers)
Closed 2 years ago.
I have an array of strings: const names = ['name1', 'name2', name3']
And I need the following array of objects:
const newArray = [{ name: 'name1' }, { name: 'name2' }, { name: 'name3' }]
How can I create newArray from names, or even possibly convert names itself without creating brand new array?
This can be easily achieved with the map function.
const newArray = names.map((name) => {
return {
name
};
});
map() can do that for you:
const names = ['name1', 'name2', 'name3'];
const newArray = names.map(x=>{return{name:x};});
console.log(newArray);

How to transform an indexed array into an associative array? [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Convert array of objects into array of properties [duplicate]
(4 answers)
Closed 2 years ago.
Is there a way to transform the following array of objects:
array = [
{ date: "08/18", id: 1 },
{ date: "08/14", id: 2 },
{ date: "08/15", id: 3 }
]
Into this? Only returning the dates:
array2 = ["08/18", "08/14", "08/15"]
I've been trying to push the date elements as it follows, but it doesn's seem to work:
array.map(e => {
array2.push(e.date)
})
Thanks in advance.
You can use the array map function:
array2 = array.map(object => object.date);

Filtering specific values on json array and create another array [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
How can i create another array by filtering one json array and include just the values pairs of a specific key?
Example (Filtering Value Pairs of Number Keys):
Array:
{ Name: 'abcd', Number: '1234' },
{ Name: 'efgh', Number: '5678' }
]````
Result Array:
````var filteredarray = ['1234','5678'];````
Thanks!
const a = [
{ Name: 'abcd', Number: '1234' },
{ Name: 'efgh', Number: '5678' }
]
function getNumbers(){
return a.map(item => item.Number);
}
getNumbers();

Check if object has particular property [duplicate]

This question already has answers here:
Get JavaScript object from array of objects by value of property [duplicate]
(17 answers)
Find object by id in an array of JavaScript objects
(36 answers)
Closed 5 years ago.
Let's say I have an array of objects
const arr = [ {name:"Bob", age: 20}, { name: "Sara", age: 22}, { name:
Tom, age:20} ];
I want to print objects with particular property, for example only those with the age == 20. So The result would be
const arr = [ {name:"Bob", age: 20}, { name: Tom, age:20} ];
I really want to do it with ES6. Do you have any suggestion what method could be used?
This will do
var filteredData = arr.filter((e) => e.age === 20)

Dynamically access object's Array property using variable [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
NOT duplicate of : Dynamically access object property using variable
How to read the JavaScript Object Array property dynamically.
var person = {
name: "Ravi",
age: 25
friends: [{
name: "Suresh"
},
{
name: "Nitin"
},
{
name: "Argha"
}
]
}
So, if I want to read any property dynamically, I can use
var dynamicProperty = 'age';
person[dynamicProperty] // Output : 25
But it fails for array.
var dynamicProperty = 'friends[1]';
person[dynamicProperty].name // Output : undefined
What is the best way to pass the name of the array dynamically ?
You can't access more than a single property at a time using dynamic property access notation. You will need to use an array of keys (often called a "path") in conjunction with Array#reduce:
var person = {
name: "Ravi",
age: 25,
friends: [{
name: "Suresh"
},
{
name: "Nitin"
},
{
name: "Argha"
}
]
}
function access (o, k) { return o[k] }
var result = ['friends', 1, 'name'].reduce(access, person)
console.log(result)

Categories

Resources