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);
Related
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]);
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
This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 3 years ago.
let's say I have an array of objects:
let arr = [
{
name: 'Jack',
id: 1
},
{
name: 'Gabriel',
id: 2
},
{
name: 'John',
id: 3
}
]
I need to check whether that array includes the name 'Jack' for example using:
if (arr.includes('Jack')) {
// don't add name to arr
} else {
// push name into the arr
}
but arr.includes('Jack') returns false, how can I check if an array of objects includes the name?
Since you need to check the object property value in the array, you can try with Array.prototype.some():
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
let arr = [
{
name: 'Jack',
id: 1
},
{
name: 'Gabriel',
id: 2
},
{
name: 'John',
id: 3
}
]
var r = arr.some(i => i.name.includes('Jack'));
console.log(r);
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);
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.