0

i have 2 array of object with diffrent key each other, i want to combine those array become 1 array,

Array of object 1

0:{materi: "materi1"}
1:{materi: "materi2"}
2:{materi: "materi3"}
3:{materi: "materi4"}

Array of object 2

0:{colour: "#FFEBCD"}
1:{colour: "#8A2BE2"}
2:{colour: "#DEB887"}
3:{colour: "#5F9EA0"}

what i have done is here push, concat but doesnt give me the output that i want to

console.log(mapped, 'mapped') 
console.log(color, 'color')
//console.log(mapped.push(color))
//console.log(mapped.push.apply(mapped, color))
// console.log(mapped.concat(color))

this is the output that i want

[{
  "color": '#E9967A',
  "materi": "Laurie Henderson"
},
{
  "color": '#8B0000',
  "materi": "Toni Ferrell"
},
{
  "color": '#9932CC',
  "materi": "Christi Summers"
},
{
  "color": '#FF8C00',
  "materi": "Adams Gray"
},
{
  "color": '#556B2F',
  "materi": "Maricela Bernard"
}]
2
  • Your input and output have different values. Also have you tried anything? Commented Nov 23, 2017 at 6:11
  • @Rajesh sorry the output just only for example that i want to so ignore the values. i tried mapped, push and concat but doesn't give me the result that i want to Commented Nov 23, 2017 at 6:14

4 Answers 4

1
var array1 = [{materi: "materi1"},
              {materi: "materi2"},
              {materi: "materi3"},
              {materi: "materi4"}];
var array2 = [{colour: "#FFEBCD"},
              {colour: "#8A2BE2"},
              {colour: "#DEB887"},
              {colour: "#5F9EA0"}];
var newArray = array1.map(function(val, ind){
    return {
        materi:val.materi,
        colour:array2[ind].colour
    }
})
Sign up to request clarification or add additional context in comments.

1 Comment

wow your so fast edit the answer, it give me knowledge about data in javasacript
1

You need to create new array with combination of the properties, they cannot automatically merge into new objects.

One way to do that is:

var arr1 = [{colour: "#FFEBCD"}], arr2 = [{materi: "materi1"}]; // your arrays

var combined = arr1.map(function(element, index){
    return {
        color: element.color,
        materi: arr2[index].materi
    }
})

Without further explanation on how you want the properties to be merged into new objects I think that's the best answer that can be given.

Also it is a bit dangerous as we cannot be sure that both arrays have the same length - so that needs to be handled somehow as well.

1 Comment

and yes those array doesn't have same length before combine both already cut one of them has same length, cause one array i set for 20 and the other dynamic from API, thanks or correction
0

You can use array#map to merge two array. Use value from the first array and using the index to get the value from the second array.

const materials = [{materi: "materi1"},{materi: "materi2"},{materi: "materi3"},{materi: "materi4"}],
      colors = [{colour: "#FFEBCD"},{colour: "#8A2BE2"},{colour: "#DEB887"},{colour: "#5F9EA0"}];
      
const result = materials.map(({materi}, index) => ({'color' : colors[index].colour, materi}));
console.log(result);

1 Comment

Its really going to be difficult for OP to understand
0

You can use Object.assign() to merge each pair together.

var materials = [{materi: "materi1"},{materi: "materi2"},{materi: "materi3"},{materi: "materi4"}];
var colors = [{colour: "#FFEBCD"},{colour: "#8A2BE2"},{colour: "#DEB887"},{colour: "#5F9EA0"}];

var merged = materials.map(function(m, i) {
  return Object.assign({}, m, colors[i]);
})

console.log(merged)

Or using es6:

let materials = [{materi: "materi1"},{materi: "materi2"},{materi: "materi3"},{materi: "materi4"}];
let colors = [{colour: "#FFEBCD"},{colour: "#8A2BE2"},{colour: "#DEB887"},{colour: "#5F9EA0"}];

let merged = materials.map((m, i) => Object.assign({}, m, colors[i]))

console.log(merged)

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.