Cannot read properties of undefined (reading 'items') [closed] - javascript

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"]

Related

JavaScript filter method not giving unique result [closed]

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 9 months ago.
Improve this question
i am trying to filter the unique ones from a given array using filter method but its throwing an empty array, here is the code
function dual(a) {
if (Array.isArray(a)) {
let unique = a.filter((val, index) => {
a.indexOf(val) == index
})
return unique;
}
}
console.log(dual([3, 1, 1, 2, 2])) //expected result [3,1,2] but showing []
You may use Set() to create a unique array:
const arr = [3,1,1,2,2]
var uniqueArray = [...new Set(arr)]
console.log(uniqueArray)
You are missing return statements from both your function and your filter array method.

Get value from an array using find in JavaScript [closed]

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 11 months ago.
Improve this question
I've got an array:
test = [{'id':'73','name':'bar', 'contact': [{'name':'barFoo','tel':'3333'}]},{'id':'45','name':'foo', 'contact':[]}]
I try to get contact.tel in first object with 'id':'73'
I'm using find method :
let contactTel = test.find(x => x.id === '73').contact.tel;
but it dosen't work. What I'm doing wrong ?
it should be like this
const test = [{'id':'73','name':'bar', 'contact': [{'name':'barFoo','tel':'3333'}]},{'id':'45','name':'foo', 'contact':[]}]
let contactTel = test.find(x => x.id === '73').contact[0].tel;
console.log(contactTel)

how to nested data from inside a JSON [closed]

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

array replaces instead of concatenating values [closed]

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

Cannot find object properties in an Javascript array [closed]

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 am currently learning JavaScript and I am having a horrible time with objects in an array.
I'm trying to find them. It won't find.
I'm trying display them, they are not displaying correctly.
I've been trying to figure this out for about 2 days and I'm lost
Here is the code
const notes = [{
title: 'The big trip',
body: ' The next big trip will be back to thailand'
},
{
title: 'Fitness goals',
body: 'really enjoying the ab programme '
},
{
title: 'life decisions',
body: 'the move overseas '
}
]
console.log(notes.length)
const findNote = function(notes, noteTitle) {
const index = notes.findIndex(function(note, index) {
return note.title === noteTitle
})
return notes[index]
}
const note = findNote(notes, 'Fitness Goals')
console.log(notes)
Your code is ok. Please check your function arguments here:
const note = findNote(notes, 'Fitness Goals')
There is no any note with title 'Fitness Goals'. Change this string to:
const note = findNote(notes, 'Fitness goals')
'Goals' and 'goals' are different strings in Javascript, so program can't find any note

Categories