How to use methods on arrays in an object [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 2 years ago.
Improve this question
myObj = {anArray: ["1", "2", "3", "4"]}
if (myObj["anArray"].includes("2")) {
console.log("hey")
}
this throws an error:
TypeError: MyObject.anArray.contains is not a function
So how do I use methods on an array in an object?
any help would be much appreciated

contains() doesn't exist for Array in javascript.
Have a look on includes() which return true or false, or indexOf() which return the position.
W3School - includes()
W3School - indexOf()

Array has no method contains use includes instead
const myObj = { anArray: ['1', '2', '3', '4'] };
if (myObj['anArray'].includes('2')) {
console.log('hey');
}

Related

Return object 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 26 days ago.
Improve this question
I need to get the result of this object, I've tried json.count(id_reported) and json['count(id_reported)'] but none worked.
const json = {
'count(id_reported)': 21
};
//console.log(json.count(id_reported));
console.log(json['count(id_reported)']);
In Javascript, Typescript and so in Express in the end, its easy to handle such things.
var myObject = {
'count': 21
}
myObject = JSON.parse(myObject);
console.log(myObject.count);
The JSON.parse is only needed, if you object is a string. Is it a Javascript object you do not need to parse.
The count(id_reported) part I don't understand. If your object looks like this in the end:
{
count(1): 1,
count(2): 2,
}
and you don't know the structure at all you can use a for loop:
for (let data in myObject) {
console.log(data); // data will be the key; so count(1) as example
}
See the in keyword in the for loop. This will give you the key. The on keyword otherwise gives the object in an array as example.

The find function is not available on my array object [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 5 months ago.
Improve this question
Consider this simple attempt to find a value in an object:
var config = {
product1: {
ids: ['master']
},
product2: {
ids: ['ps1', 'ps2']
}
};
var id = 'ps2';
var slotID = 'master';
var categorySlotIds = config[slotID].ids;
categorySlotIds.find(id);
I get: TypeError: Cannot find function find in object product1, product2
If I do typeof(categorySlotIDs) the result is object.
The manual for find says: The find() method returns the first element in the provided array
What gives?

I'm having a hard time understanding why this is returning x is not a function [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'm having a hard time understanding why this is returning "5Style is not a function"
NationalLevelCategoriesChosenList = [
["5Style", "5MelodyHarmony", "5RhythmTempo", "5TextureStructureForm", "5Timbre"]
], [
[]
];
if (NationalLevelCategoriesChosenList[0].some("5Style")) {
console.log("working")
}
The some() function allows you to test a value with a provided function. I think you want to use includes() instead ? Please see snippet below.
NationalLevelCategoriesChosenList = [
["5Style", "5MelodyHarmony", "5RhythmTempo", "5TextureStructureForm", "5Timbre"]
], [
[]
];
if (NationalLevelCategoriesChosenList[0].includes('5Style')){
console.log("working")
}
See the documentation for some:
The some() method tests whether at least one element in the array passes the test implemented by the provided function.
The argument you are passing "5Style" is a string, not a function.
You are probably looking for the includes method:
NationalLevelCategoriesChosenList[0].includes("5Style")
But if you wanted to use some then you need to write a function:
NationalLevelCategoriesChosenList[0].some((current_value) => curent_value === "5Style")

array.map is not a function [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 keep getting this error
escalationreport.js:62 Uncaught (in promise) TypeError: results.map is not a function
{escalated_by: "Osten Diniz", ticket_Number: "SHA-194414", escalation_reason: "Closing sale", date: "2020-03-31T16:25:30.000Z"}
This is console log in the browser level
I am trying to map these values to a table but fails with the above error
I found that data.map is only possible for arrays but don't know how to convert that json to array ({} to [])
You can use Object.keys() or Object.entries() and then use map
Object.entries(yourObject).map((arr) => {
// arr[0] will have the key and arr[1] will have the value of a key-value
// pair of yourObject.
console.log(arr)
})
var result = {escalated_by: "Osten Diniz", ticket_Number: "SHA-194414",
escalation_reason: "Closing sale", date: "2020-03-31T16:25:30.000Z"};
// one
var keys= Object.keys(result);
keys.map(v => {
console.log("KEY ========>",v);
});
// two
var entries= Object.entries(result);
entries.map(v => {
console.log("KEY ========>",v[0],"VALUE ========>",v[1]);
});

how to assign values to undefined objects? [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 6 years ago.
Improve this question
var data = {};
data.info.id = "alpha";
This logs to the console: "TypeError: data.info is undefined".
Well that's great and all but I need to store a value in data.info.id. Isn't that what objects are supposed to do?
This should produce an object that looks like this:
data: {
info: {
id: "alpha"
}
}
Is data.info = {} really a necessary step?
In response to Patrick Evans - that's an unrelated question.
Well there is another way. That's putting the info-object directly in the data-object like this:
var data = {
info: {}
}
data.info.id = "alpha";
console.log(data);

Categories

Resources