2

how do you produce these objects ?

let obs = [
    { car: cars[0], color: colors[0] },
    { car: cars[1], color: colors[1] },
    { car: cars[2], color: colors[2] }
]

from these arrays without hard coding

let cars = ["audi", "audi", "audi"]
let colors = ["darkgrey", "red", "silver"]
3
  • Possible duplicate of How do I zip two arrays in JavaScript? Commented Nov 9, 2018 at 16:56
  • 1
    I just need to clarify something, will cars and colors be the same length at all times? Commented Nov 9, 2018 at 16:56
  • One-off code may not be an issue for a one-off problem, but writing generic code is usually better since reusability is always a factor of managing complexity. Commented Nov 9, 2018 at 18:06

5 Answers 5

4

Use Array.map() to produce an array of objects:

const cars = ["audi", "audi", "audi"]
const colors = ["darkgrey", "red", "silver"]

const result = cars.map((car, i) => ({
  car,
  color: colors[i]
}));

console.log(result);

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

Comments

1

You can use a forEach loop with an index and push new objects by choosing corresponding elements from each array:

let cars = ["audi", "audi", "audi"];
let colors = ["darkgrey", "red", "silver"];

let result = [];
cars.forEach((car, i) => {
  result.push({car, color: colors[i]});
});

console.log(result);

1 Comment

Array.prototype.map would also work well here - that way you don't have to manually define the array/push items :)
1

There are numerous way of doing it. Personally I will prefer using a array map function, which will create a new array .

You can apply map method on any of the array and use it's index to retrieve value from another array

let cars = ["audi", "audi", "audi"]
let colors = ["darkgrey", "red", "silver"];

let result = cars.map((item, index) => {
  return {
    car: item,
    color: colors[index]
  }
});

console.log(result)

You can also use conventional for loop , array forEach & array reduce also

Comments

1

Spent five more minutes, this worked too thanks Guys :)

let cars = ["audi", "audiXL", "audiRover"]
let colors = ["silver", "darkgrey", "white"]

function matchCars () {   

    let matches = []

    for(let i = 0; i < cars.length; i++) {  
        let object = {car: cars[i], color: colors[i]}
        matches.push(object)
    }
    return matches

}

const answer = matchCars()
console.log(answer);

Comments

0

This question is really: How do I convert a structure of arrays to an array of structures?

A simple, general, solution looks like:

let aos = soa2aos(
  [ 'car',  ['Civic', 'Integra'] ],
  [ 'year', [1994, 2001] ],
  [ 'color', ['grey', 'orange'] ]
);

console.log(aos);

function soa2aos(...soa) {
  return soa[0].reduce((aos, arr, i) => {
    let o = aos[i] = {};
    soa.forEach(([name, dat]) => o[name] = dat[i]);
    return aos;
  }, []);
}

Read more about Parallel Arrays

I hope this helps!

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.