values not updating with increment in i [duplicate] - javascript

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

Related

how can use string for access value of object? [duplicate]

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

How to check if array of object contains a string [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 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);

How can I access the property of a nested object in Javascript? [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Accessing properties of a variable object with JavaScript
(4 answers)
Closed 6 years ago.
I need some help with accessing the property value of an object that is nested within another object.
I have this code:
var userStats = {
'Jacob': {
visits: 1
},
'Owen': {
visits: 2
},
'James': {
visits: 3,
},
'Ann': {
visits: 4
}
};
What I want to do is access the value of the visits.
I have tried:
for(var firstName in customerData){
console.log(firstName.visits);
}
But it is not working. It outputs 'undefined'.
Any help is greatly appreciated.
Where firstName is a string which is the property name(or key) of the object so get object using the string.
for(var firstName in customerData){
console.log(customerData[firstName].visits);
}
var customerData = {
'Jacob': {
visits: 1
},
'Owen': {
visits: 2
},
'James': {
visits: 3,
},
'Ann': {
visits: 4
}
};
for (var firstName in customerData) {
console.log(customerData[firstName].visits);
}
I have made customerData an array of objects 'customers' and you can iterate them easily by swapping in for of in the for loop.
for (var customer of customerData)
Example:
var customerData = [
{
firstName: 'Jacob',
visits: 3
},
{
firstName: 'bocaj',
visits: 2
}
];
for (var customer of customerData) {
console.log(customer.firstName, customer.visits);
}

What's the JavaScript way of updating an array of objects from another array? [duplicate]

This question already has answers here:
JavaScript merging objects by id [duplicate]
(18 answers)
Closed 6 years ago.
I have an object called probe. It has a property called sensors which is an array.
var probe = {
sensors = [
{ id: 1, checked: false },
{ id: 2, checked: true },
{ id: 3, checked: false },
... //more sensors
],
... //other properties
}
I have separate array which has an updated list of sensors like below.
var updatedSensors = [
{ id: 1, checked: true },
{ id: 3, checked: true }
];
I want to update the sensors array in the probe object from the values in the updatedSensors. How would I do that?
This can be easily achieved by using a couple of for-loops. But for-loops are not the pretty way of iterating in JavaScript, so I was wondering how to do this the preferred way.
Edit:
The objects in the updatedSensors is a subset of objects in probe.sensors. In other words, updatedSensors does not have all the objects (ids) that are there in the probe.sensors, but probe.sensors has all the objects (ids) that are in the updatedSensors.
Thanks.
Try this bit of code:
probe.sensors.map(function (sensor) {
// Loop through updated sensors & match sensor.id so we can reassign val
updatedSensors.map(function (f) {
if (f.id == sensor.id) {
sensor.checked = f.checked
}
})
return sensor;
})

Get value by property from javascript object list [duplicate]

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.

Categories

Resources