This question already has answers here:
how to access object property using variable [duplicate]
(2 answers)
Closed 8 years ago.
i want to get property value from javascript object list, i have a list
var cars = [{ id: 1, name: 'Audi' }, { id: 2, name: 'BMW' }, { id: 1, name: 'Honda' }];
Now i want get id or name property value by using for loop like this
var cars = [{ id: 1, name: 'Audi' }, { id: 2, name: 'BMW' }, { id: 1, name: 'Honda' }];
var items=[];
var firstProp='id';
for (var i = 0; i < model.length; i++) {
//original work
items.push({ value: model[i].firstProp});//here is the problem
}
please give good advise, thanks.
If I understand your problem correctly, you should use square bracket notation instead of dot notation:
//..
items.push({ value: model[i][firstProp]});//here is the problem
You should do like this
items.push({ value: model[i][firstProp]});
the . notation expects the firstProp to be present as a key in dictionary, since the firstProp is a variable that contains a string you should use [] notation.
Related
This question already has answers here:
How to find object in array by property in javascript?
(3 answers)
Closed 4 months ago.
Here's a list of objects for example:
const products = [
{
name: "Cherry",
price: 10,
quantity: 0,
productId: 1,
image: "../images/cherry.jpg"
},
{
name: "Orange",
price: 15,
quantity: 0,
productId: 2,
image: "../images/orange.jpg"
},
{
name: "Strawberry",
price: 3,
quantity: 0,
productId: 3,
image: "../images/strawberry.jpg"
}
];
I want to get the other properties of an object from the list using its productId. I'm a beginner in JavaScript so I can't seem to figure this out. Any help would be highly appreciated!
products.find((product) => product.productId === pid) // where pid is the id of the product you want to find
What i understand from your description is that you want to find out the product property of a specific items from the list for that you can use
==ES6==
products.filter( (item) => { return item.productId==productId})
==Vanila js==
products.filter(function(item) { return item.productId==productId})
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 months ago.
at first please see image then, how can use for example:
use this humans.body."name" instead of this humans.body.name? is there any way to solve that?
my codes:
//this is my file
let fileJson = {
status: 200,
result: {
author_id: "137",
text: "1281ms",
author: "Ctrl+Z",
},
};
let data = [{id: 1, key: "author_id"}];
console.log(fileJson.result.author_id); //output: 137
console.log(data[0].key); //output: author_id
console.log(fileJson.result.data[0].key); //output: Uncaught TypeError ...
//how can solve this error?
thank you :)
try using this notation to access the property:
fileJson.result[data[0].key]
Values can be accessed a couple of ways. Use the array accessor with the key value.
//this is my file
let fileJson = {
status: 200,
result: {
author_id: "137",
text: "1281ms",
author: "Ctrl+Z",
},
};
let data = [{
id: 1,
key: "author_id"
}];
console.log(fileJson.result.author_id); //output: 137
console.log(data[0].key); //output: author_id
console.log(fileJson.result[data[0].key]);
This question already has answers here:
Check if an array contains any element of another array in JavaScript
(32 answers)
Closed 1 year ago.
I have these 2 arrays
The first one is fixed and does not change and contains the id of the 2nd array:
fixed=["123","456","789"]
The second can change
variableArray=[{name:"Joe",id:"123"},{name:"Joe",id:"456"},{name:"Joe",id:"789"}]
I want to return true if, even if there were some changes at the end the variable array is the same length and contains exactly the same keys of the "fixed"
NOT VALID:
fixed=["123","456","789"]
variableArray=[{name:"Joe",id:"456"},{name:"Joe",id:"789"}]
return false because is missing the id "123" (and the length is also different so is excluded by default)
NOT VALID:
fixed=["123","456","789"]
variableArray=[{name:"Joe",id:"123"},{name:"Joe",id:"456"},{name:"Joe",id:"001"}]
this will return false because, even if contains 3 elements as there are in the "fixed" is missing the id "789" and have another "001" instead
as #mplungjan mentiond, you can use Every:
let fixed = ["123", "456", "789"];
let variableArray1 = [{
name: "Joe",
id: "123"
}, {
name: "Joe",
id: "456"
}, {
name: "Joe",
id: "789"
}];
let variableArray2 = [{
name: "Joe",
id: "123"
}, {
name: "Joe",
id: "456"
}, {
name: "Joe",
id: "001"
}]
let containsAll1 = variableArray1.every(elem => fixed.includes(elem.id));
let containsAll2 = variableArray2.every(elem => fixed.includes(elem.id));
console.log(containsAll1, containsAll2);
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 3 years ago.
let's say I have an array of objects:
let arr = [
{
name: 'Jack',
id: 1
},
{
name: 'Gabriel',
id: 2
},
{
name: 'John',
id: 3
}
]
I need to check whether that array includes the name 'Jack' for example using:
if (arr.includes('Jack')) {
// don't add name to arr
} else {
// push name into the arr
}
but arr.includes('Jack') returns false, how can I check if an array of objects includes the name?
Since you need to check the object property value in the array, you can try with Array.prototype.some():
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
let arr = [
{
name: 'Jack',
id: 1
},
{
name: 'Gabriel',
id: 2
},
{
name: 'John',
id: 3
}
]
var r = arr.some(i => i.name.includes('Jack'));
console.log(r);
This question already has answers here:
loop and push object into an array
(3 answers)
Closed 7 years ago.
I'm new to JavaScript. Here is the piece of code I'm trying to understand. I'm incrementing i value changing the name and id in img then pushing it to array data. As show in output, value is same throughout the loop. why isn't name and id changing with i. I'm unable to figure it out. Need help?
my code is
var data=[]
var img={}
for(var i =0 ;i<5;i++){
img.name="i"+i;
img.id=i;
data.push(img);
}
console.log(data);
ouput is :
[ { name: 'i4', id: 4 },
{ name: 'i4', id: 4 },
{ name: 'i4', id: 4 },
{ name: 'i4', id: 4 },
{ name: 'i4', id: 4 } ]
try this code please it worked and tested :
var data=[];
for(var i =0 ;i<5;i++){
var img={};
img.name="i"+i;
img.id=i;
data.push(img);
}
console.log(data);