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 = ?;
}
abbotto
  • 4,259
  • 2
  • 21
  • 20
user3702643
  • 1,465
  • 5
  • 21
  • 48
  • 4
    Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – Jared Smith Jun 28 '17 at 16:41
  • The suggested solutions are for jQuery. Will they work for javascript too? @JaredSmith – user3702643 Jun 28 '17 at 16:45
  • 1
    [Array#find](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find) – Xotic750 Jun 28 '17 at 16:46
  • [Array#find](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find) is not jQuery. I actually don't see any jQuery solutions suggested (yet). And remember, jQuery *is* JavaScript. – Mark Rabey Jun 28 '17 at 17:17

5 Answers5

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>
Xotic750
  • 22,914
  • 8
  • 57
  • 79
0

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

Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
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')
Ved
  • 11,837
  • 5
  • 42
  • 60
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

abbotto
  • 4,259
  • 2
  • 21
  • 20
  • 1
    It would be better to use [Array#some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) than [Array#forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) – Xotic750 Jun 28 '17 at 16:58
  • What if the value for `i` was `0`? – Xotic750 Jun 28 '17 at 17:53
  • The correct value for "0" should be returned - now that I've updated the answer. :) – abbotto Jun 28 '17 at 18:35
  • The keywords `const` and `let`, and `Arrow functions` can cause an issue in some browsers. ;) – Xotic750 Jun 28 '17 at 18:41
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);
elqueso101
  • 84
  • 3