0

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 = ?;
}
4
  • 4
    Possible duplicate of Find object by id in an array of JavaScript objects Commented Jun 28, 2017 at 16:41
  • The suggested solutions are for jQuery. Will they work for javascript too? @JaredSmith Commented Jun 28, 2017 at 16:45
  • 1
    Array#find Commented 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. Commented Jun 28, 2017 at 17:17

5 Answers 5

1

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>

Sign up to request clarification or add additional context in comments.

4 Comments

While this answer does find the value of "i", the items array doesn't match the example that was given by the OP.
No, because this was posted before the Q was edited. :)
There can be issues with calling "hasOwnProperty" in some browsers. Example: stackoverflow.com/questions/8157700/…
Like I care about IE8. :D
0

Just use ObjectName[Key] this is enoughto get you the value Like b[123]

Comments

0

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

the output i get for this is [] 'obj'. Any idea why?
@user3702643 have you run the snippet.
I ran it on my local environment. Cant seem to get the same result.
Array#filter will always give you an array, and it may contain 0 or 1 or more elements. It's probably not the best choice in this use case.
@Xotic750 correct. but we dont know if the ID is duplicate.
|
0
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

0

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

Documentation for "Array.some" method

4 Comments

It would be better to use Array#some than Array#forEach
What if the value for i was 0?
The correct value for "0" should be returned - now that I've updated the answer. :)
The keywords const and let, and Arrow functions can cause an issue in some browsers. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.