Javascript loop through object keys [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I have the following javascript object
const items = {
"item 1":{name:'test',vals:[{name:'first',....}]},
"item 2":{name:'test3',vals:[{name:'second', ....}]}
//other keys
}
The object keys are dynamic and not just "item 1" and 2 and they keep on changing. I want to access the name property of each object. SO i have done the following by am stuck
Object.keys(items).forEach(key=>{
let keyitem = items.key //am stuck on how to pass the key
})
How do i proceed to the dynamic key as the items.key is undefined as there is no such key in the items object.How do i proceed.

Replace items.key as items[key]
Object.keys(items).forEach(key=>{
let keyitem = items[key]
})

Related

Access value in array object [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I have this object:
var myValues = {
55bdf7bda89de40349854077: ["hello"]
55be0c77a89de403498540bc: ["goodbye"]
55be0e22a89de403498540c1: ["hey there!"]
}
And a variable as contains an id:
var id = '55be0e22a89de403498540c1';
I want to find in the object by this id and get the value in array.
I try to find with:
myValues.id[0]
but ... not works ;/
Anybody can help me?
You need to do myValues[id] or myValues['55be0e22a89de403498540c1']. It's a json object, not a primitive array so you can't access it with indexes like you are trying to do.
Your array should like,
var myValues = {
'55bdf7bda89de40349854077': "hello",
'55be0c77a89de403498540bc': "goodbye",
'55be0e22a89de403498540c1': "hey there!"
}
alert(myValues["55be0e22a89de403498540c1"])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
May this will help you

How to loop through this javascript and get the (id) and (brandName) [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
How to do i get the id value and brandName value
[{"id":24,"brandName":"BMW"},{"id":25,"brandName":"Mercedes Benz"}]
Not sure what you're trying to do as your question is very vague. But if you want to iterate over each element and do something with the values you could do something like this:
let items = [{"id":24,"brandName":"BMW"},{"id":25,"brandName":"Mercedes Benz"}];
items.forEach(elem => {
const {id, brandName} = elem;
console.log(`ID: ${id}. Brand Name: ${brandName}.`);
})

Get object element from a defined string [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
how to get data from object with define string?
case:
var data = [
{name:"Sharma",country:"India"},
{name:"Udin",country:"Indonesia"},
{name:"John Carter",country:"Mars"}
];
getData(data,"country");
function getData(data,element){
console.log(data[1].element);
}
i want to get country but result is undefinded, how to fix this?
You would need to know both the index and the property
function getData(data,index,element){
console.log(data[index][element]);
}
getData(data,1,"country");
function getData(data,element){
console.log(data[1][element]);
}
That's the correct way to access the value by using a key that is a string.

How to use an object's value as the parameter for another object in Javascript? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 6 years ago.
If I have:
var key = "Section";
course.key = "101";
I get told course.key is unidentified. I'm trying to set course.Section = "101". Is there anyway to pass key's value in the second line?
The property key of course is undefined. Use bracket notation.
course[key] = "101"
is the same as
course["Section"] = "101"

Accessing element by variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have an array and want to access its data. However, I need to use a variable instead of the name to access the data.
For example;
My data:
$scope.myData = {
"user": [
{ child[{......}],
..........
}
],"user2": [
{
child[{......}],
.........
}
],...........
The following works
console.log("lenght:"+$scope.myData.user[0].child.length);
but I want to use a variable instead of user[0], because it is dynamic, it changes every time.
Similar to
var m=user;
console.log("lenght:"+$scope.myData.m[0].child.length);
How about this?
var m = 'user';
console.log("lenght:"+ $scope.myData[m][0].child.length );

Categories