0

I have a multidimensional javascript array of objects that I am trying to use to simply collate both the id and its key within the unit array to a brand new array

What is the best solution for returning the id with the key within its units array but reversed so the key of the new array is the unit id

[
  {
    units: [
      {
        id: 10000282,
        name: "Group 1",
      },
      {
        id: 10000340,
        name: "Group 2",
      },
      {
        id: 10000341,
        name: "Group 3",
      },
    ],
  },
  {
    units: [
      {
        id: 10000334,
        name: "Group 4",
      },
    ],
  },
]

Expected output - just return an array in the following format e.g

ids = [ 10000282 => 0, 10000340 => 1, 10000341 => 2, 10000334 => 0 ]

so 10000282 would be the key, and 0 would be the value for the first iteration of the array -- update -- I probably didn't explain the output so well the output should be as follows but in an array format.

ids[10000282] = 0
ids[10000340] = 1
ids[10000341] = 2
ids[10000334] = 0
3
  • You mean output as an array of strings? Commented Aug 3, 2021 at 9:41
  • an array output so that I could call something like ids[10000341] in the console and it would return the value 2 Commented Aug 3, 2021 at 9:43
  • Ah, sounds like you want the result to be an object then, not an array. Commented Aug 3, 2021 at 9:47

4 Answers 4

2

So, I suppose you want to get the results back into a dictionary with key the nested id and value its index in the wrapping units. You can easily do that as follows:

x = [
  {
    units: [
      {
        id: 10000282,
        name: "Group 1",
      },
      {
        id: 10000340,
        name: "Group 2",
      },
      {
        id: 10000341,
        name: "Group 3",
      },
    ],
  },
  {
    units: [
      {
        id: 10000334,
        name: "Group 4",
      },
    ],
  },
];

result = x.flatMap(el => el.units.map((e,i) => ({[e.id]: i})));

console.log(result);

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

1 Comment

this is great although i just realised this returns a index that begins from 0 (I probably didn't explain this so well first time round) - I changed the latter part of the description in the original question - how do I amend your solution so that it is simple array that I can call the first index of the array like ids[10000340] & this returns 1
2

Here's a slightly different approach using reduce:

const data = [{
    units: [{
        id: 10000282,
        name: "Group 1",
      },
      {
        id: 10000340,
        name: "Group 2",
      },
      {
        id: 10000341,
        name: "Group 3",
      },
    ],
  },
  {
    units: [{
      id: 10000334,
      name: "Group 4",
    }, ],
  },
];

const result = data.reduce(
  (total, current) =>
    total.concat(current.units.map(({ id }, i) => ({ [id]: i }))),
  []
);

console.log(result);

1 Comment

good example using reduce i'll try them both thank you
1

It sounds like you want to the able to access the id properties directly which points to refactoring into an object or Map.

Using an Object created using Object.fromEntries()

const arr = [{ units: [{ id: 10000282, name: "Group 1", }, { id: 10000340, name: "Group 2", }, { id: 10000341, name: "Group 3", },], }, { units: [{ id: 10000334, name: "Group 4", },], },];

const result = Object.fromEntries(arr.flatMap(({ units }) => units.map(({ id }, i) => [id, i])));

console.log(result);
// { '10000282': 0, '10000334': 0, '10000340': 1, '10000341': 2 }

console.log('result[10000340] = ', result[10000340])
// result[10000340] = 1

Using a Map

const arr = [{ units: [{ id: 10000282, name: "Group 1", }, { id: 10000340, name: "Group 2", }, { id: 10000341, name: "Group 3", },], }, { units: [{ id: 10000334, name: "Group 4", },], },];

const result = new Map(arr.flatMap(({ units }) => units.map(({ id }, i) => [id, i])));
// Map(4) { 10000282 => 0, 10000340 => 1, 10000341 => 2, 10000334 => 0 }

console.log('result.get(10000340) = ', result.get(10000340))
// result.get(10000340) =  1

2 Comments

Oh man that is amazing code - this is what I needed sorry I didn't explain it to well - but that is fantastic @pilchard this is the precise thing I was trying to achieve. Thanks to everyone that got me here :)
Kudos for giving the two examples - I used the Map one in the end :)
1

const arrays = [
  {
    units: [
      {
        id: 10000282,
        name: "Group 1",
      },
      {
        id: 10000340,
        name: "Group 2",
      },
      {
        id: 10000341,
        name: "Group 3",
      },
    ],
  },
  {
    units: [
      {
        id: 10000334,
        name: "Group 4",
      },
    ],
  },
];

const results = arrays.flatMap((items) => items.units.map(({id}, index) => `ids[${id}] = ${index}`));

console.log(...results);

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.