friedman
friedman

Reputation: 675

Convert associative array to standard array in Javascript

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?

Upvotes: 0

Views: 270

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1074335

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.

Upvotes: 3

Keith
Keith

Reputation: 24191

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);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386604

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; }

Upvotes: 2

Related Questions