1

I just posted a question here ( Sorting/Filtering from 2 arrays ) but I ran into a problem because my id's could be random strings:

so I have a master array with all the data :

var masterArray = [
        {'id' : 'wedfd', 'title' : 'Title 1'},
        {'id' : 'hji', 'title' : 'Title 2'},
        {'id' : 'sdfds', 'title' : 'Title 3'},
        {'id' : 'fgfgf', 'title' : 'Title 4'},
        {'id' : 'kkd', 'title' : 'Title 5'},
        {'id' : 'jjj', 'title' : 'Title 6'},
        {'id' : 'abc', 'title' : 'Title 7'}
    ];

I get an array with this info :

var sortFilterInfo = [
    {'id' : 'jjj', 'sortOrder' : 1},
    {'id' : 'hji', 'sortOrder' : 2},
    {'id' : 'abc', 'sortOrder' : 3}
]

With this information I need an array which gives me this sorted filtered array: ( I am only using native DOM Array methods (ES6) (map/filter/sort) and NOT Jquery,lodash, etc.

var resultArray = [
    {'id' : 'jjj', 'title' : 'Title 6', 'sortOrder' : 1},
    {'id' : 'hji', 'title' : 'Title 2', 'sortOrder' : 2},
    {'id' : 'abc', 'title' : 'Title 7', 'sortOrder' : 3}
]

Thanks!

3
  • You should post what you've tried as well Commented Mar 6, 2016 at 5:29
  • so in the other post the masterArray ids were all integers. so the answer from @Andrew Mast worked great. but now I have id's that are just random strings Commented Mar 6, 2016 at 5:34
  • All the solutions posted below seem to be O(n^2) in general or O(n*m). I wonder if there is a better solution to this problem. Commented Mar 6, 2016 at 6:30

2 Answers 2

1

You can use map() and find() ( using ES6 arrow notation )

var masterArray = [{
  'id': 'wedfd',
  'title': 'Title 1'
}, {
  'id': 'hji',
  'title': 'Title 2'
}, {
  'id': 'sdfds',
  'title': 'Title 3'
}, {
  'id': 'fgfgf',
  'title': 'Title 4'
}, {
  'id': 'kkd',
  'title': 'Title 5'
}, {
  'id': 'jjj',
  'title': 'Title 6'
}, {
  'id': 'abc',
  'title': 'Title 7'
}];

var sortFilterInfo = [{
  'id': 'jjj',
  'sortOrder': 1
}, {
  'id': 'hji',
  'sortOrder': 2
}, {
  'id': 'abc',
  'sortOrder': 3
}]

// if `sortFilterInfo` is not sorted then sort it using sort()
// sortFilterInfo.sort((a,b) => a.id-b.id)


// iterate over `sortFilterInfo` array for generating sorted array
var res = sortFilterInfo.map(v => {
  // get element from `masterArray` based on the id
  var obj = masterArray.find(v1 => v1.id == v.id);
  // add sortOrder to the object
  obj.sortOrder = v.sortOrder;
  // return updated object
  return obj;
});

document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');

Or the following with an array of id and later get the index using indexOf()

var masterArray = [{
  'id': 'wedfd',
  'title': 'Title 1'
}, {
  'id': 'hji',
  'title': 'Title 2'
}, {
  'id': 'sdfds',
  'title': 'Title 3'
}, {
  'id': 'fgfgf',
  'title': 'Title 4'
}, {
  'id': 'kkd',
  'title': 'Title 5'
}, {
  'id': 'jjj',
  'title': 'Title 6'
}, {
  'id': 'abc',
  'title': 'Title 7'
}];

var sortFilterInfo = [{
  'id': 'jjj',
  'sortOrder': 1
}, {
  'id': 'hji',
  'sortOrder': 2
}, {
  'id': 'abc',
  'sortOrder': 3
}]

// if `sortFilterInfo` is not sorted then sort it using sort()
// sortFilterInfo.sort((a,b) => a.id-b.id)

// create an array with all id
var idArr=masterArray.map(v=>v.id);


// iterate over `sortFilterInfo` array for generating sorted array
var res = sortFilterInfo.map(v => {
  // get element from `masterArray` based on the id
  var obj = masterArray[idArr.indexOf(v.id)];
  // add sortOrder to the object
  obj.sortOrder = v.sortOrder;
  // return updated object
  return obj;
});

document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');

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

1 Comment

@29er : glad to help
1

Here is a kind of brute force approach. If sortFilterInfo is in order, this solution will always work. Basically just check (lowest to highest order) if it is within the masterArray, if it is, it is guaranteed to be pushed into the correct place of the result array.

var masterArray = [
  {'id' : 'wedfd', 'title' : 'Title 1'},
  {'id' : 'hji', 'title' : 'Title 2'},
  {'id' : 'sdfds', 'title' : 'Title 3'},
  {'id' : 'fgfgf', 'title' : 'Title 4'},
  {'id' : 'kkd', 'title' : 'Title 5'},
  {'id' : 'jjj', 'title' : 'Title 6'},
  {'id' : 'abc', 'title' : 'Title 7'}
];

var sortFilterInfo = [
  {'id' : 'jjj', 'sortOrder' : 1},
  {'id' : 'hji', 'sortOrder' : 2},
  {'id' : 'abc', 'sortOrder' : 3}
];

var resultArray = [];

for(var i = 0; i < sortFilterInfo.length; i++) {
  for(var j = 0; j < masterArray.length; j++) {
    if (sortFilterInfo[i].id === masterArray[j].id) {
      resultArray.push({id : masterArray[j].id, title: masterArray[j].title, sortOrder: sortFilterInfo[i].sortOrder});
    }
  }
}

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.