0

Which is the best way to "convert" associative array to standard (0 based indexes) array. Is there any other way than iteration and rewriting each item?

I have an array which was created by counting appearances of some properties and it is needed that way. The output array lookes like:

let ASSARR = { 
    "abc": { "c": 5, "p": 3 },
    "def": { "c": 1, "p": 10 },
    "ghi": { "c": 15, "p": 7 }
};

...and so. I need to filter and sort it though then I need to "convert" it to standard array so it looked more like this:

let STARR = [
    { "i": "abc", "c": 5, "p": 3 },
    { "i": "def", "c": 1, "p": 10 },
    { "i": "ghi", "c": 15, "p": 7 }
];

Do I need to iterate by for or similar loop or maybe there is more effective way to do this?

1
  • Just to help you find questions, answers, articles, and such on this sort of thing in the future: In JavaScript, that's called an object, not "associative array," not least because objects are more than just bags of name/value pairs. :-) Commented Dec 13, 2022 at 16:59

3 Answers 3

3

Is there any other way than iteration and rewriting each item?

No, you'll need a loop (either in your code, or in code in the standard library that you call such as looping through the result of Object.entries).

Since it's a loop either way, I'd probably just write the loop (especially as doing so, you can loop once rather than multiple times). Here I'm assuming you want to create new objects rather than just adding an i property to the objects you have (but keep reading):

const result = [];
for (const i in ASSARR) {
    result.push({
        i,
        ...ASSARR[i],
    });
}

let ASSARR = { 
    "abc": { "c": 5, "p": 3 },
    "def": { "c": 1, "p": 10 },
    "ghi": { "c": 15, "p": 7 }
};
const result = [];
for (const i in ASSARR) {
    result.push({
        i,
        ...ASSARR[i],
    });
}
console.log(result);
.as-console-wrapper {
    max-height: 100% !important;
}

...but if you want to modify the existing objects instead:

const result = [];
for (const i in ASSARR) {
    const object = ASSARR[i];
    object.i = i;
    result.push(object);
}

let ASSARR = { 
    "abc": { "c": 5, "p": 3 },
    "def": { "c": 1, "p": 10 },
    "ghi": { "c": 15, "p": 7 }
};
const result = [];
for (const i in ASSARR) {
    const object = ASSARR[i];
    object.i = i;
    result.push(object);
}
console.log(result);
.as-console-wrapper {
    max-height: 100% !important;
}


Note: In the above, I'm assuming there are no inherited but enumerable properties you want left out. If there are, wrap the push calls in if (Object.hasOwn(object, i)) to skip inherited properties.

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

Comments

2

You could get the entries and map the objects with key.

const
    object = { abc: { c: 5, p: 3 }, def: { c: 1, p: 10 }, ghi: { c: 15, p: 7 } },
    array = Object
        .entries(object)
        .map(([i, o]) => ({ i, ...o }));

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

Comments

0

You can just use Object.entries, and Array.map..

eg..

let ASSARR = { 
    "abc": { "c": 5, "p": 3 },
    "def": { "c": 1, "p": 10 },
    "ghi": { "c": 15, "p": 7 }
};

let STARR = Object.entries(ASSARR).map(([k,v]) => {
  return {i: k, ...v};
});

console.log(STARR);

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.