1

I have an array like this:

[
    { "avl_res": 1, "res_status": "Available", "code": "AAA" },
    { "avl_res": 2, "res_status": "Unavailable", "code": "AAA" },
    { "avl_res": 2, "res_status": "Available", "code": "BBB" },
    { "avl_res": 1, "res_status": "Available", "code": "CCC" },
    { "avl_res": 5, "res_status": "Unavailable", "code": "CCC" },
    { "avl_res": 3, "res_status": "Unavailable", "code": "DDD" },
];

I am trying to produce this:

[
    {"avl_res":1,"total_res":3,"code":"AAA"},
    {"avl_res":2,"total_res":2,"code":"BBB"},
    {"avl_res":1,"total_res":6,"code":"CCC"},
    {"avl_res":0,"total_res":3,"code":"DDD"},
];

Following the answer to this question, I managed to do it with this:

var singles = {};
arr.forEach(function (item) {
    var single = singles[item.code] = singles[item.code] || {};
    single[item.res_status] = item.avl_res;
});

var outputList = [];
for (var single in singles) {
    let total_res = 0;
    let avl_res = 0;

    singles[single]['Available'] ? avl_res = singles[single]['Available'] : avl_res = 0;
    singles[single]['Unavailable'] ? total_res = avl_res + singles[single]['Unavailable'] : total_res = avl_res;

    outputList.push({ code: single, total_res: total_res, avl_res: avl_res });
}

console.log(outputList);

Just wondering if this is efficient enough or there's a better/elegant way, maybe by using other JS functions (e.g. reduce, map, etc.). Cheers!

2
  • 2
    I’m voting to close this question because this questions belongs on Code Review. Commented Apr 26, 2021 at 5:44
  • Noted @adiga. I'll post this kind of question in the proper section next time. Thanks! Commented May 6, 2021 at 7:48

1 Answer 1

1

If you care code performance (efficiency) that much, You would like this...

const arr = [
    { "avl_res": 1, "res_status": "Available", "code": "AAA" },
    { "avl_res": 2, "res_status": "Unavailable", "code": "AAA" },
    { "avl_res": 2, "res_status": "Available", "code": "BBB" },
    { "avl_res": 1, "res_status": "Available", "code": "CCC" },
    { "avl_res": 5, "res_status": "Unavailable", "code": "CCC" },
    { "avl_res": 3, "res_status": "Unavailable", "code": "DDD" },
];

const results = [];
const findItem = {};

for (const { avl_res, code, res_status } of arr) {
    let item = findItem[code];
    const res = res_status == 'Available' ? avl_res : 0;
    if (item) {
        item.total_res += avl_res;
        item.avl_res += res;
    }
    else {
        item = { avl_res: res, total_res: avl_res, code };
        results.push(item);
        findItem[code] = item;
    }
}
console.log(results);

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

4 Comments

Why not use a plain find object instead of adding that as a property to array?
There is no problem to use find() But In this case, I just want to write super efficiency code, Even we should trust efficiency about build in function in JavaScript, Although find() is not just loop, it use function as loop, and function call is expensive...
I didn't mean array.find method. I meant why not use a const find = {} object directly instead of adding that property to the array like results.find . Arrays shouldn't be assigned anything other than an integer property
Although in my case, Its fine to do, But for your (@adiga) sake, I edited my answer...

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.