Hi really new to javascript and am hoping to get some help with a problem im facing.
So I basically have an array that stores objects. Each object contains an id and a variable i which is a number. My question is this: how can I extract the value of i from the object array with the id value? The id that I am using would already have been stored in the array with an i value.
var i = 1;
var id;
var b = {};
var y = [];
if(condition) {
b = {"123":i};
y.push(b);
}
if(condition) {
id = 123;
//Find corresponding i value for id "123" from object array y
i = ?;
}
-
4Possible duplicate of Find object by id in an array of JavaScript objectsJared Smith– Jared Smith2017年06月28日 16:41:59 +00:00Commented Jun 28, 2017 at 16:41
-
The suggested solutions are for jQuery. Will they work for javascript too? @JaredSmithuser3702643– user37026432017年06月28日 16:45:04 +00:00Commented Jun 28, 2017 at 16:45
-
1Array#findXotic750– Xotic7502017年06月28日 16:46:49 +00:00Commented Jun 28, 2017 at 16:46
-
Array#find is not jQuery. I actually don't see any jQuery solutions suggested (yet). And remember, jQuery is JavaScript.Mark Rabey– Mark Rabey2017年06月28日 17:17:31 +00:00Commented Jun 28, 2017 at 17:17
5 Answers 5
An example with Array#find
var hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
var i = 1;
var id;
var b = {};
var y = [];
var condition = true;
if (condition) {
b = {
"123": i
};
y.push(b);
}
if (condition) {
id = 123;
// Find corresponding i value for id "123" from object array y
// i = ? ;
var found = y.find(function(o) {
return hasOwn(o, id);
});
var f = found ? found[id] : found;
console.log(f);
}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.9/es5-shim.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.9/es5-sham.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.js"></script>
<script type="text/javascript" src="https://wzrd.in/standalone/es7-shim@latest"></script>
4 Comments
Just use ObjectName[Key] this is enoughto get you the value
Like b[123]
Comments
Many ways to do this. Here is one of them.
var arr = [{id:1},{id:123}];
var obj = arr.filter(function(val){
if(val.id===123)
return val
})
console.log(obj,'obj')
7 Comments
const stuff = [
{
name: 'Leonardo',
id: 100
},
{
name: 'Donatello',
id: 101
},
{
name: 'Raphael',
id: 102
},
{
name: 'Michaelangelo',
id: 103
},
];
First, use the Array.prototype.find() method on the array to find the object within it that has the desired ID and store it in the entry variable. Then, log the value corresponding to the name key within that object.
const desired = 102;
const entry = stuff.find(item => item.id === desired);
console.log(entry.name);
Comments
You can loop through the array and get the object property value as follows:
var arr = [
{"123": "valueA"},
{"456": "valueB"}
];
const id = "123";
let value;
arr.some(obj => {
if (obj[id] || obj[id] === 0) value = obj[id];
});
console.log(value);
4 Comments
i was 0?const and let, and Arrow functions can cause an issue in some browsers. ;)