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

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)

Related

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

Change array in to object in react js [duplicate]

This question already has answers here:
How do I convert array of Objects into one Object in JavaScript?
(17 answers)
Convert Javascript array of objects into one object
(4 answers)
Closed 3 years ago.
This is my array format
let array = [
0: {
key:"name",
value: "John"
},
1: {
key:"age",
value:25
},
2: {
key:"job",
value:"software engineer"
},...
];
Change this array to object in given below format
{
name: "John",
age: 27,
job: "software engineer"
}
You can achieve it using forEach on the array.
Give this a try:
const array = [{
key: "name",
value: "John"
}, {
key: "age",
value: 25
}, {
key: "job",
value: "software engineer"
}];
const expected = {};
array.forEach(item => expected[item.key] = item.value);
console.log(expected);
You can use Array.prototype.reduce() to do this:
let array = [
{
key:"name",
value: "John"
},
{
key:"age",
value:25
},
{
key:"job",
value:"software engineer"
}
];
let result = array.reduce((a,b) => {
a[b.key] = b.value;
return a;
}, {});
console.log(result);
All you need to do is to loop over the array using forEach, for loop, while loop etcand push the values in a new object.
Also make sure that your array syntax is correct because the way you have mentioned it in the question is incorrect.
const data = [{ key:"name", value: "John" },{ key:"age", value:25 },{ key:"job", value:"software engineer" } ];
const res = {};
data.forEach(item => { res[item.key] = item.value});
console.log(res);

Javascript's .includes function not working correctly with array of objects [duplicate]

This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 4 years ago.
I have an array of objects which I'm using the .includes() function. I'm searching this array with an object that is in the array (Objects are identical). However there doesn't appear to be a match. I have replicated the problem in this fiddle. The code is also below. So what is the correct way to check if an array contains am object?
let list1 = [{
name: "object1"
},
{
name: "object2"
},
{
name: "object3"
},
{
name: "object4"
}
]
if (list1.includes({
name: "object1"
})) {
document.write('contains')
} else {
document.write('doesnt')
}
You can't compare objects directly, but using this method , you can compare them with JSON.stringify.
let list1 = [{
name: "object1"
},
{
name: "object2"
},
{
name: "object3"
},
{
name: "object4"
}
]
var contains = list1.some(elem =>{
return JSON.stringify({name: "object1"}) === JSON.stringify(elem);
});
if (contains) {
document.write('contains')
} else {
document.write('doesnt')
}
You can try following
let list1 = [{name:"object1"},{name:"object2"},{name:"object3"},{name:"object4"}]
if (list1.some(({name}) => name === "object1")) {
document.write('contains')
} else {
document.write('doesnt')
}

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)

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

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

Categories

Resources