3

I already have an object witch has two arrays:

const services = {
        iconAndLink: [
            'Icon1',
            'Icon2',
            'Icon3',
        ],
        name: [
            'Name1',
            'Name2',
            'Name3',
        ],
    };

I looked into Object.assign(), array.reduce(), map etc... and can't seem to find a decent answer here that merges these two.

For final result I need:

services = [
        {
            icon: 'Icon1',
            name: 'Name1'
        },
        {
            icon: 'Icon2',
            name: 'Name2'
        },
        {
            icon: 'Icon3',
            name: 'Name3'
        },
    ]

Note that I need to have the icon and name keys.

Is this even possible in js?

5
  • Yes it is possible. However, will the two arrays always be of the same length? Commented May 28, 2021 at 20:13
  • Does this thread help? stackoverflow.com/questions/39127989/… Commented May 28, 2021 at 20:13
  • @CroogQT See that there's a static key here. Just name and icon. Commented May 28, 2021 at 20:14
  • You can't use the same variable services for the source and result, since it's declared const. Commented May 28, 2021 at 20:16
  • Why couldn't you do it with map()? Commented May 28, 2021 at 20:17

4 Answers 4

4

This should work

const services = {
  iconAndLink: ["Icon1", "Icon2", "Icon3"],
  name: ["Name1", "Name2", "Name3"],
};

let result = services.iconAndLink.map(function (icon, index) {
  return { icon: services.iconAndLink[index], name: services.name[index] };
});


console.log(result);

Make sure both arrays same the same length and both are ordered

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

2 Comments

@Kinglish posted this with a forEach. What difference does this make with using map?
Both are simillar, but different. Map is more the functional way, and create a new array. forEach allows you to change the original array. The map function will receive a function that will run on each item on array and create a new array with all created objects. You can read more here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… For performance point, map is slight faster (as seen here: morioh.com/p/b2d058745cb8)
1

A simple forEach loop using index would do the trick

const services = {
  iconAndLink: [
    'Icon1',
    'Icon2',
    'Icon3',
  ],
  name: [
    'Name1',
    'Name2',
    'Name3',
  ],
};

let newarray = [];
services.iconAndLink.forEach((el, index) => newarray.push({
      icon: el,
      name: services.name[index]
    })
    );

    console.log(newarray)

Comments

1

const services={iconAndLink:["Icon1","Icon2","Icon3"],name:["Name1","Name2","Name3"]};
    
const res = services.name.map((e, i) => ({
  icon: e,
  name: services.iconAndLink[i]
}))

console.log(res)

Comments

1

Assuming the arrays are of the same size, you can do:

const services = { iconAndLink: [ 'Icon1', 'Icon2', 'Icon3', ], name: [ 'Name1', 'Name2', 'Name3', ], };
const newArr = [];

for (let i = 0; i < services.iconAndLink.length; i++) {
    newArr.push({icon: services.iconAndLink[i], name: services.name[i]})
}

console.log(newArr)

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.