Bill Morris
Bill Morris

Reputation: 589

Select subset of objects from javascript object array

I've got a long array of objects:

var myObj = {
  a: "aaa",
  b: "bbb",
  c: "ccc",
  d: "ddd",
  ...
}

I'd like to create a new object array that consists of every object except the first two:

var myObj2 = {
  c: "ccc",
  d: "ddd",
  ...
}

Obviously myObj.slice(2) doesn't work (as much as I'd like it to), and I can't select them all by name (myObj.c, myObj.d, etc) because there are more than 100 objects, and potentially more to be added.

What's the best way to select a range of objects in a object array?

Upvotes: 1

Views: 2586

Answers (3)

jmargolisvt
jmargolisvt

Reputation: 6088

You may want to recompose this, but here is a general approach.

First, here's your object with some sortable keys:

var obj = {B201: "apple", B342: "orange", B123: "pear"}

Then create an array of the first two keys after sorting:

var arr = Object.keys(obj).sort().slice(0,2);

This returns ["B123", "B201"].

Define a function that removes the object property based on the key name:

function remove (e, i, a) {delete obj[e];};

Then call that function for each property:

arr.forEach(remove);

You should get the object with the "first two" properties removed:

Object {B342: "orange"}

Upvotes: 2

Taj Ahmed
Taj Ahmed

Reputation: 895

You can use below method to remove the keys you don't want

var myObj = {a: "aaa",b: "bbb",c: "ccc",d: "ddd"}

function filterObject(obj,filterObject){
    var newObj = {}
    for(key in obj){
        if(!filterObject[key]){
            newObj[key] = obj[key];
        }
    }
    return newObj;
}

console.log(filterObject(myObj,{a:true,b:true}))

Sample Output:

{ c: 'ccc', d: 'ddd' }

Upvotes: 2

Prakash Rao
Prakash Rao

Reputation: 2398

Suppose you have an array of objects like this

myObj = [
                {
                  a: "aaa",
                  b: "bbb",
                  c: "ccc",
                  d: "ddd",
                  ...
                },

                {
                  a: "aaa",
                  b: "bbb",
                  c: "ccc",
                  d: "ddd",
                  ...
                },

                {
                  a: "aaa",
                  b: "bbb",
                  c: "ccc",
                  d: "ddd",
                  ...
                },

                {
                  a: "aaa",
                  b: "bbb",
                  c: "ccc",
                  d: "ddd",
                  ...
                },
            ];

Now run a loop for main array

Inside that loop through each object inside array. Increment a counter in each turn and check it is greater than 2 then insert it in main array myObj again

$.each(myObj, function( index, value ) {
    var counter = 0;
    $.each(value, function( index2, value2 ) {
        counter++;
        if(counter > 2){
            myObj[] = value;
        }
    });

});

if you have different structure for the main array of object then slight change will be there

Upvotes: 1

Related Questions