skip to Main Content
let obj =[
    {
        "SID": 123,
        "EMPLOYEE_NAME": "Test123",
        "EMPLOYEE_ID": 1
    },
    {
        "SID": 543,
        "EMPLOYEE_NAME": "Test1543",
        "EMPLOYEE_ID": 2
    },
    {
        "SID": 454,
        "EMPLOYEE_NAME": "Test1454",
        "EMPLOYEE_ID": 3
    },
    {
        "SID": 789,
        "EMPLOYEE_NAME": "Test1789",
        "EMPLOYEE_ID": 4
    },
    {
        "SID": 999,
        "EMPLOYEE_NAME": "Test1999",
        "EMPLOYEE_ID": 5
    },
    {
        "SID": 555,
        "EMPLOYEE_NAME": "Test1555",
        "EMPLOYEE_ID": 6
    },
    ]

let sidNumbers = "789,543,123";

function newArr(arr, obj){
    let newwArr = [];
    let splitArr = arr.split(',');
    splitArr.reduce((curr, acc)=>{
        if(`${acc['SID']}`.includes(curr))
        {
            newwArr.push(acc)
        }
    },obj)
    return newwArr;
}

console.log(newArr(sidNumbers, obj));

the first output firstArray = [{
        "SID": 789,
        "EMPLOYEE_NAME": "Test1789",
        "EMPLOYEE_ID": 4
    },
    {
        "SID": 543,
        "EMPLOYEE_NAME": "Test1543",
        "EMPLOYEE_ID": 2
    },
    {
        "SID": 123,
        "EMPLOYEE_NAME": "Test123",
        "EMPLOYEE_ID": 1
    }
]

the output of secondArray =[
 
    {
        "SID": 454,
        "EMPLOYEE_NAME": "Test1454",
        "EMPLOYEE_ID": 3
    },
    {
        "SID": 999,
        "EMPLOYEE_NAME": "Test1999",
        "EMPLOYEE_ID": 5
    },
    {
        "SID": 555,
        "EMPLOYEE_NAME": "Test1555",
        "EMPLOYEE_ID": 6
    },
]

I have an array of object and string of numbers. Trying to create two new array of objects. first in which sidNumbers matches from the obj it filters an return the an array of object and in second the sidNumbers doesn’t matches from the obj it filters an return the an array of object. Is using reduce is the best way to solve this problem or is there any other way to solve this problem?

2

Answers


  1. I would suggest you to use filter instead of reduce too get a clearer code.

    The only advantage of reduce is that it would iterate over the list only once, which only is relevant if you have a huge list (like, hundreds of thousands of elements)

    In any case, here is how you could do I with reduce:

    [firstArray, secondArray] = obj.reduce(
      ([y,n], o) => sidNumbers.contains(o.SID) ? [[...y, o],n] : [y,[...n, o]], [[],[]])
    

    Notes on the code above:

    • [firstArray, secondArray] = ... – this is destructuring assignment. It means that I expect that the result of the expression will be an array with 2 elements, so I am assigning the first element into the variable firstArray and the second to secondArray.
    • obj.reduce(([y,n], o) => ..., [[],[]]) – the function reduce takes 2 arguments. The first is a function and the second is the initial value (in this case, I am using an array of 2 empty arrays as initial value). The function must take 2 input parameters:
      • the accumulated aggregation so far (that I know is always an array with 2 elements, so I am destructuring them into 2 variables y and n
      • the current object o of the iteration over obj
    • sidNumbers.contains(o.SID) – checking if the attribute SID of the current object is included in your sidNumbers list.
    • ... ? [[...y, o],n] : [y,[...n, o]], [[],[]] – this is the ternary conditional operator. If the condition returns true, the function will return the value before the colon :, which is just appending the current object o into the array y. If it is false, append o into the second array n.
    • At the end, after iterating over the original list, all elements where the condition was true (i.e., their SID is contained in the reference list) will be aggregated in the first array, and the others on the second array.
    Login or Signup to reply.
  2. Use filter() and includes() instead of reduce(). Reduce is better suited for modifying elements to a new array.

    let obj =[
        {
            "SID": 123,
            "EMPLOYEE_NAME": "Test123",
            "EMPLOYEE_ID": 1
        },
        {
            "SID": 543,
            "EMPLOYEE_NAME": "Test1543",
            "EMPLOYEE_ID": 2
        },
        {
            "SID": 454,
            "EMPLOYEE_NAME": "Test1454",
            "EMPLOYEE_ID": 3
        },
        {
            "SID": 789,
            "EMPLOYEE_NAME": "Test1789",
            "EMPLOYEE_ID": 4
        },
        {
            "SID": 999,
            "EMPLOYEE_NAME": "Test1999",
            "EMPLOYEE_ID": 5
        },
        {
            "SID": 555,
            "EMPLOYEE_NAME": "Test1555",
            "EMPLOYEE_ID": 6
        },
        ]
    
    const filterObjectsBySID = (obj, sidNumbers) =>
      [obj.filter(({ SID }) => sidNumbers.includes(SID.toString())),
      obj.filter(({ SID }) => !sidNumbers.includes(SID.toString()))];
    
    const sidNumbers = "789,543,123";
    
    const [matchingObjects, nonMatchingObjects] = filterObjectsBySID(obj, sidNumbers);
    
    console.log(matchingObjects);
    console.log(nonMatchingObjects);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search