Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a nested array. It is about user information. I need to take the age from the user and add it to my array. I have tried some code approch. Did not work.
Here is the Array
var userData = [
{
"key" : "user A",
"value" : [
{
"id" : 1,
"location" : "NYC"
},
{
"id" : 2,
"profession": "programmer"
}
]
}
]
JS:
userData.value.push({"age" : 25})
I want to add the age afte id 2 object
You have to access the first element of userData:
userData[0].value.push({"age" : 25})
you use userdata[0] to push value at last in array object
userData[0].value.push({"age" : 25})
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
const data = [{
"detectedLanguage": {
"language": "hi",
"score": 1
},
"translations": [{
"text": "long steff",
"to": "en"
}]
}]
How do I console.log only the translation.text field?
I tried console.log(JSON.stringify(res.data.translation.text) but that gives me the following error:
TypeError: Cannot read property 'text' of undefined
Try this,
console.log(data[0].translations[0].text)
No need to use Json.stringify, because data[0].translations[0].text is not a Json, it is a string.
You created arrays and am missing the index and the "s" in translations:
console.log(JSON.stringify(data[0].translations[0].text))
I also don't know why you're referencing it as res.data
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have other JSON data similar to this, hence, I would like to filter it.
const data = [{
"1": {items: { Part1: true, Part2: true } },
id: "zaMR9TR7hNV3p3VFNumyNbXMto93",
displayName: name1
}]
However, I keep getting the error of
Cannot read properties of undefined (reading 'items')
const filter = data.filter(
(a) =>
a.["1"].items.Part1 == true
);
How do I fix this?
you have an extra "." right before ["1"]
a["1"].items.Part1
By the way, if i were you, i would rename items by "item", because it's not an array, just an object.
You are accessing elemet incorrectly. please use this
const filter = data.filter(
(a) =>
a["1"].items.Part1 == true
);
error was here, the extra dot(.) after a => a.["1"], it should be a["1"]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have this array declared as
rowData = []
I want to update this array with a forloop but it keeps replacing the values instead of updating it. I guess I'm doing something wrong.
rowDataDefs(){
for (let to of this.detailedOrders){
console.debug(to)
this.rowData = [
{table_no: to.table.tableNo, order_no: to.orderNo, date_time: to.table.createdDate }
]
}
}
rowDataDefs(){
for (let to of this.detailedOrders){
console.debug(to)
this.rowData.push(
{table_no: to.table.tableNo, order_no: to.orderNo, date_time: to.table.createdDate });
}
}
You need to use array.push() instead of replacing the same index(0th index) value every time.
rowDataDefs(){
for (let to of this.detailedOrders){
console.debug(to)
this.rowData.push(
{
table_no: to.table.tableNo,
order_no: to.orderNo,
date_time: to.table.createdDate
}
)
}
return this.rowData;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have an javascript array of objects as follows :
list = [
{
"employee_work_rights_id":74,
"amazon_id":173,
"employee_id":3,
"work_rights":"australian_citizen",
"document_type":"password",
"display_name":"a.pdf",
"filename":"abc.pdf",
"s3bucket":"xyz",
"filepath":"employer27\/employee3\/"
},
{
"employee_work_rights_id":75,
"amazon_id":175,
"employee_id":3,
"work_rights":"australian_citizen",
"document_type":"password",
"display_name":"a.pdf",
"filename":"xyz.pdf",
"s3bucket":"zyx",
"filepath":"employer27\/employee3\/"
}
]
I tried to access amazon_id as follows :
console.log(list[0].amazon_id);
This is giving me undefined. How can I access this ?
You're doing the right thing. The way you initialize list is correct, and so is the way you access the property.
Assuming you were trying interactively in the console, the undefined you see is not the value of list[0].amazon_id. It's the return value of console.log. In Javascript, everything has a return value. However, in the console, just above or just below your undefined, you should see amazon_id's proper value.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I was trying to make some object into objects , but there's a problem with my syntax.
That's the code:
var friends = {
bill : {
firstName:"Bill",
lastName:"Gates",
number:00158965478
},
steve :{
firstName:"Steve",
LastName:"Jobs"
number:00125688977
},
Joe: {
firstName:"Joe",
LastName:"Erabti",
number:0021625804429
}
};
You forgot the comma after last names of Jobs.. So its suppose to be like this:
var friends = {
bill : {
firstName:"Bill",
lastName:"Gates",
number:00158965478
},
steve :{
firstName:"Steve",
LastName:"Jobs", //<-- You previously forgot it here
number:00125688977
},
Joe: {
firstName:"Joe",
LastName:"Erabti",
number:0021625804429
}
};