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

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

Related

Why does pushing this object into my array give me a number? [duplicate]

This question already has answers here:
Why does Array.prototype.push return the new length instead of something more useful?
(6 answers)
javascript push returning number instead of object [duplicate]
(1 answer)
Pushing objects in an array only returns last object pushed
(5 answers)
How to use Array.push and return the pushed item?
(3 answers)
Closed 2 years ago.
So, I was building a simple rest api on node, I fixed the problem, but I was just curious as to why I even get a number 4 to begin with? You'll know what I mean when you look at the code, It's just a small snippet of code that I'm confused about.
main.js
const people = [
{ id: 1, firstName: "Daniel"},
{ id: 2, firstName: "Erika" },
{ id: 3, firstName: "Christian"},
];
let person = people.push({ id: people.length + 1, firstName: "Mark"})
If I console.log(person) I get 4 as a value. I mean I understand that If I console.log(people) I will get what I added, but I'm just curious as to why when I console.log(person) I get a value of 4?
Ciao, as #VLAZ said, Array.push returns the new length of your people array.
If you want to get the last person inserted in people array you could do:
const people = [
{ id: 1, firstName: "Daniel"},
{ id: 2, firstName: "Erika" },
{ id: 3, firstName: "Christian"},
];
let person = people[people.push({ id: people.length + 1, firstName: "Mark"}) - 1];
console.log(person)
-1 because the 4th element of the array is the object in position 3

How to access property on JSON with Arrays and Objects [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I have this JSON:
[
{ someTitle: 'NAME OF SomeTitle' },
[
{
id: '7oiqxhRXRqEV0D70G',
photo: 'https://co/image/ab67616d739a3e7d0c38c3af225e4695ce',
jugement: 'GOAl',
Name: 'Some Name.'
}
],
{
token: 'BQAVhYiUweHGTTlIIZHthAKzulZ-DHg'
}
]
This comes from a request I make to my node Server. If I do a console.log(req.body) I get the information above.
So when I try to do console.log(req.body.token) I get undefined.
How can I access the token property then?
The size of the JSON may change , so I can't just access it by index.
Since it is an array of objects, and the element you are looking for is at the 3th position of the array. You need to call it using index = 2 (because index starts with 0).
After that, you can access token property.
const res = [{
someTitle: 'NAME OF SomeTitle'
},
[{
id: '7oiqxhRXRqEV0D70G',
photo: 'https://co/image/ab67616d739a3e7d0c38c3af225e4695ce',
jugement: 'GOAl',
Name: 'Some Name.'
}],
{
token: 'BQAVhYiUweHGTTlIIZHthAKzulZ-DHg'
}
]
console.log(res[2].token)
Check this out
console.log(req.body[2]["token"])
If Array size is not fixed then try this
let req = {
body: [
{ someTitle: "NAME OF SomeTitle" },
[
{
id: "7oiqxhRXRqEV0D70G",
photo: "https://co/image/ab67616d739a3e7d0c38c3af225e4695ce",
jugement: "GOAl",
Name: "Some Name.",
},
],
{
token: "BQAVhYiUweHGTTlIIZHthAKzulZ-DHg",
},
],
};
let data = req.body;
let token = "";
for (let each of data) {
if (each.hasOwnProperty("token")) {
token = each["token"];
break;
}
}
console.log(token)

How to get Index of an object from an array [JavaScript]? [duplicate]

This question already has answers here:
indexOf method in an object array?
(29 answers)
Closed 5 years ago.
let database = [{
name: 'James Bond',
code: '007'
},
{
name: 'El',
code: '11'
}
]
let subject = {
name: 'James Bond',
code: '007'
}
database.indexOf(subject)
This returns -1. I found similar questions here on SO but they were all mostly asking to find index of the object by comparing it to a key value pair.
From DOCS
The indexOf() method returns the index within the calling String
object of the first occurrence of the specified value, starting the
search at fromIndex. Returns -1 if the value is not found.
Use findIndex
DEMO
let database = [{
name: 'James Bond',
code: '007'
},
{
name: 'El',
code: '11'
}
]
let subject = {
name: 'James Bond',
code: '007'
}
console.log(database.findIndex(x => x.name=="James Bond"))
let subInd ;
database.forEach(function(ele,index){
if(ele.name == subject.name && ele.code == subject.code ){
subInd=index;
}
});
console.log(subInd);

values not updating with increment in i [duplicate]

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

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