0

Im just looking for a good way to do this:

I have 2 arrays:

 var allItems = [
            {'id' : '1', 'title' : 'Title 1' },
            {'id' : '2', 'title' : 'Title 2' },
            {'id' : '3', 'title' : 'Title 3' },
            {'id' : '4', 'title' : 'Title 4' },
            {'id' : '5', 'title' : 'Title 5'},
            {'id' : '6', 'title' : 'Title 6' }
        ];

    var idsToExtract = ['1', '3', '6'];

I am looking for a result with two arrays that look like this:

    var array1 = [
            {'id' : '1', 'title' : 'Title 1' },
            {'id' : '3', 'title' : 'Title 3' },
            {'id' : '6', 'title' : 'Title 6'}
        ];

    var array2 = [
            {'id' : '2', 'title' : 'Title 2' },
            {'id' : '4', 'title' : 'Title 4' },
            {'id' : '5', 'title' : 'Title 5'}
        ];

I am ONLY using native JS ( ES2015 ) array methods.( map/filter etc).

NO jquery/underscore etc.

Thanks!

3 Answers 3

3

The Array.prototype.filter() method can help you:

var allItems = [
            {'id' : '1', 'title' : 'Title 1' },
            {'id' : '2', 'title' : 'Title 2' },
            {'id' : '3', 'title' : 'Title 3' },
            {'id' : '4', 'title' : 'Title 4' },
            {'id' : '5', 'title' : 'Title 5'},
            {'id' : '6', 'title' : 'Title 6' }
        ];

var idsToExtract = ['1', '3', '6'];

var array1 = allItems.filter(function(item) {
  return idsToExtract.indexOf(item.id) !== -1;
});

var array2 = allItems.filter(function(item) {
  return idsToExtract.indexOf(item.id) === -1;
});

Of course you can use Array.prototype.foreach() method, and create the 2 arrays in one iteration cycle:

var allItems = [
            {'id' : '1', 'title' : 'Title 1' },
            {'id' : '2', 'title' : 'Title 2' },
            {'id' : '3', 'title' : 'Title 3' },
            {'id' : '4', 'title' : 'Title 4' },
            {'id' : '5', 'title' : 'Title 5'},
            {'id' : '6', 'title' : 'Title 6' }
        ];

var idsToExtract = ['1', '3', '6'],
  array1 = [],
  array2 = [];

allItems.forEach(function(item) {
  idsToExtract.indexOf(item.id) !== -1 ? array1.push(item) : array2.push(item);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Use Array.prototype.filter():

var array1 = allItems.filter((x) => idsToExtract.includes(x.id));
var array2 = allItems.filter((x) => !idsToExtract.includes(x.id));

JSFiddle: https://jsfiddle.net/ykcdvp9v/1/

2 Comments

@29er yeah, ES6 really makes the one liners clean and fun!
ES2015 has includes(). Use it...
0

Using simple for loop

var allItems = [
            {'id' : '1', 'title' : 'Title 1' },
            {'id' : '2', 'title' : 'Title 2' },
            {'id' : '3', 'title' : 'Title 3' },
            {'id' : '4', 'title' : 'Title 4' },
            {'id' : '5', 'title' : 'Title 5'},
            {'id' : '6', 'title' : 'Title 6' }
        ];

var idsToExtract = ['1', '3', '6'],

    var remainingItemsArray= [];
    var extractedItemsArray= [];
    for(var i=0;i<allItems.length;i++){
            var item = allItems[i];
            if(idsToExtract.indexOf(item.id) != -1){
                remainingItemsArray.push(item);
            }else{
                extractedItemsArray.push(item);
            }        
    }

Comments

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.