0

Let us assume that we have an array

  var mRegions = [];

This array get's populated with following object

  $.ajax(settings).done(function (response) {

                        for (var i in response.items[0].devices)
                        {

                            mRegions.push(
                                    {
                                        id: response.items[0].devices[i].id,
                                        name: response.items[0].devices[i].name,
                                        description: response.items[0].devices[i].description,
                                        uid: response.items[0].devices[i].beacon.iBeacon.uid,
                                        major: response.items[0].devices[i].beacon.iBeacon.major,
                                        minor: response.items[0].devices[i].beacon.iBeacon.minor,

                                    });
                        }



     var myId = 'b1';

At some point of time, I need to get an object from this array of objects whose id matches with the given id (myID)

Is there a way to achieve this without a for...in loop?

0

3 Answers 3

3

Array.prototype.find(). Make sure you read the browser compatibility section.

myRegions.find(region => region.id === myId)

or the legacy version

myRegions.find(function(r){return r.id === myId})

let myRegions = [{
  id: 'a1',
  name: 'A 1'
}, {
  id: 'a2',
  name: 'A 2'
}, {
  id: 'a3',
  name: 'A 3'
}, {
  id: 'b1',
  name: 'B 1'
}, {
  id: 'b2',
  name: 'B 2'
}];

let myId = 'b1';

let pre = document.createElement('pre');
pre.textContent = JSON.stringify(myRegions.find(region => region.id === myId), null, '  ');
document.body.appendChild(pre);

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

3 Comments

@Rajesh Arrow functions are easily translated and if OP needs a polyfill, there's one in the linked documentation ~ developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
I know that polyfill link is available, but if OP is not aware about it, he/she will use it directly and expect it to work in all browsers and when it fails on IE then they will look for polyfill. So just give a heads-up that this will happen
@Rajesh I don't think it's unreasonable to expect people to read the documentation, especially if they encounter an error
1

If it is an option, you can use an object istead of an array, and set the keys as ids.

var mRegions = {};

$.ajax(settings).done(function (response) {
    for (var i in response.items[0].devices) {
        mRegions[response.items[0].devices[i].id] = {
          id: response.items[0].devices[i].id,
          name: response.items[0].devices[i].name,
          description: response.items[0].devices[i].description,
          uid: response.items[0].devices[i].beacon.iBeacon.uid,
          major: response.items[0].devices[i].beacon.iBeacon.major,
          minor: response.items[0].devices[i].beacon.iBeacon.minor,
        };
      }
    });

Then access the values with the following notation:

var region = mRegions.theId

Or:

var myId = 123;
var region = mRegions[myId];

Comments

1
let array = [
   {id: 1, name: 'Name1'}, 
   {id: 3, name: 'Name3'}, 
   {id: 5, name: 'Name5'}, 
   {id: 7, name: 'Name7'}
];

let result = array.filter(function( obj ) {
  return obj.id === 5;
});

Also consider .find() function from @Phil answer.

.filter definition:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

.find definition:

The find() method returns a value in the array, if an element in the array satisfies the provided testing function. Otherwise undefined is returned.

2 Comments

A minor update: let result = array.filter(function( obj ) { return obj.id === 5; });
@RDX didn't noticed I have just 2x =, thanks for it. Edited

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.