0
initialArray = [ obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8 ]; // all eight are uniq objects
filterdArray = [ obj2, obj5, obj6, obj8 ]; // varies from 0 to 8

varOrder = [ 6, 8, 3, 2, 1, 4, 5, 7 ]

I have a varOrder as input, which is from 1 to 8. It is always 1-8 but in different order as per user wish.

Initially the Array have Obj1 to Obj8, but after filter, the Array may or may not have elements, If it has elements, it varies from 1 to 8.

what am I looking for is if

filterdArray = [ obj2, obj5, obj6, obj8 ]; // and my varOrder is
varOrder = [ 6, 8, 3, 2, 1, 4, 5, 7 ]; // required ouput is like below

FinalObjects = [ obj6, obj8, obj2, obj5 ]

In simple, out of 8 in the order give by user, if some objects are not available skip to next object.

The Reasoning for FinalObjects = [ obj6, obj8, obj2, obj5 ] is

varOrder | FilteredArray

6 | obj6 available from FilterdArray (obj6)
8 | obj8 available from FilteredArray (obj8)
3 | obj3 is not available from FilteredArray
2 | obj2 is available from FilteredArray (obj2)
1 | obj1 is not available from FilteredArray
4 | obj4 is not available from FilteredArray
5 | obj5 is available from FilteredArray (obj5)
7 | obj7 is not availabe from FilteredArray

Thus FinalObjects = [ obj6, obj8, obj2, obj5 ]

3
  • 3
    What have you tried so far? Commented Jan 20, 2022 at 19:34
  • Please explain the reasoning behind the contents of FinalObjects. Commented Jan 20, 2022 at 19:35
  • @ScottHunter, 1. varOrder = 6,8,3,2,1,4,5,7 2. filterdArray has obj 2,5,6,8. 3. Match ordernum 6 to obj6. ordernum 8 to obj8 ignore ordernum 3 as obj3 is not available. Match ordernum 2 to obj2 so on. Commented Jan 20, 2022 at 19:41

3 Answers 3

2

Perform the mapping first, then filter.

const varOrder = [ 6, 8, 3, 2, 1, 4, 5, 7 ];
const initialArray = [ obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8 ];

const orderedArray = varOrder.map((idx) => initialArray[idx - 1]);
// [ obj6, obj8, obj3, obj2, obj1, obj4, obj5, obj7 ]

const filteredArray = orderedArray.filter(obj => obj !== null);
// Assume all objects are null except for 6, 8, 2, and 5.
// [ obj6, obj8, obj2, obj5 ]
Sign up to request clarification or add additional context in comments.

1 Comment

this is brilliant. It took me hours to understand your code as I am startup learner. It worked and I understood the concept. Thank You.
1

Hope this is the expected result. just used a for loop

let obj1 = {name: 'obj1'};let obj2 = {name: 'obj2'};let obj3 = {name: 'obj3'};let obj4 = {name: 'obj4'};let obj5 = {name: 'obj5'};let obj6 = {name: 'obj6'};let obj7 = {name: 'obj7'};let obj8 = {name: 'obj8'}

initialArray = [ obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8 ]; // all eight are uniq objects
filterdArray = [ obj2, obj5, obj6, obj8 ]; // varies from 0 to 8

varOrder1 = [ 6, 3, 8, 2, 1, 4, 5, 7 ];
varOrder2 = [ 7, 5, 4, 1, 2, 8, 3, 6 ];

let finalArr1 = [];
let finalArr2 = [];

const sorter = (varOrder) => {
    let finalArr = []
  for (let i = 0; i < varOrder.length; i++) {
  
    if (filterdArray.includes(initialArray[varOrder[i]-1])){
        finalArr.push(initialArray[varOrder[i]-1])
    }
   }
   return finalArr;
}       

console.log(sorter(varOrder1))

console.log(sorter(varOrder2))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

You can use this approach:

First, we map each element of filterdArray to an order index, since this is an expensive operation, and we do not want to run it every time we rearrange the array.

Then we sort the mapped array by the order index.

Finally, we return the original objects.

const initialArray = [{n:'obj1'}, {n:'obj2'}, {n:'obj3'}, {n:'obj4'}, {n:'obj5'}, {n:'obj6'}, {n:'obj7'}, {n:'obj8'}];
const filterdArray = [{n:'obj2'}, {n:'obj5'}, {n:'obj6'}, {n:'obj8'}]; 
const order = [ 6, 8, 3, 2, 1, 4, 5, 7 ];

const mapped = filterdArray.map((obj) => (
  { obj, order: order.at(initialArray.findIndex((item)=> item.n === obj.n)) }));

console.log('mapped >>', mapped);

const sortedByOrder = mapped
  .sort((o1, o2) => o1.order - o2.order)
  .map(({obj}) => obj);
  
console.log('sortedByOrder >>', sortedByOrder);
.as-console-wrapper {max-height: 100% !important; top: 0}

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.