The find function is not available on my array 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 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?

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.

How to use methods on arrays in an 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 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');
}

How can i show data present inside the nested elements using map 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 4 years ago.
Improve this question
Please tell me how can i access the nested objects in arrays
I have pasted the code below and I want to access the nested objects using
only through map function.
Data=[
{name:'Amir'},
{name:'jamile'},
{name:'hali'}
]
const result=Data.name.map( (name) => name);
console.log(result);
You can do it either by accessing the property using . operator or by destructing.
let Data=[
{name:'Amir'},
{name:'jamile'},
{name:'hali'}
]
const result=Data.map( ({name}) => name);
console.log(result);
const result2=Data.map( (name) => name.name );
console.log(result2);

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

Javascript String.replace [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 8 years ago.
Improve this question
Can someone please explain why this code isn't working?
(it has been simplified for this example)
$(document).ready(function () {
var test = 'broken';
test = test.replace('broken','working');
console.log(test); // working
var field = $('[for="tournament_name"]').html();
console.log(field); // Tournament Name:
console.log(typeof field); // string
field = field.relpace(':',''); // Uncaught TypeError: undefined is not a function
});
I don't understand why it is saying replace() is undefined?
I did read through the docs, what am I missing here?
Maybe it's a typo:
relpace --> replace

Categories

Resources