How to Combine Two Arrays into One Object With Key Value Pair

how to combine two arrays into one object with key value pair?

Use Array.reduce

let a = ['name', 'options', 'address', 'options', 'gender', 'options'];let b = ['john doe', 'a', 'india', 'b', 'male', 'c'];
let r = a.reduce((o,c,i) => {o[c] = o[c] ? o[c] + ", " + b[i]:b[i]; return o;}, {})console.log(r);

Combine two arrays into key-value pairs and add values if key already exists

You need to check if the key already exists in the object so you don't overwrite the previous value

var a = ["red", "green", "red", "blue"]
var b = [2, 4, 3, 1]

var obj = {};
for (var i=0; i<a.length; i++) {
// if it doesn't exist assign value 0 to it, otherwise use existing value
obj[a[i]] = obj[a[i]] || 0;
// then add the new value
obj[a[i]] += b[i];
}

console.log(obj)

Merge two array object : one has key value pair and another is just array

This is how i would do it:

const arrayZ = [];

const sort = () =>{

arrayX.forEach(element => {
let store_info = [];
arrayY.forEach(el =>{
if(element.store_ids.includes(el.store_id)){
store_info.push(el);
console.log(store_info)
}
})
element.store_info = store_info;
arrayZ.push(element);
})
}

sort();

console.log(arrayZ);

You can even refactor the function a bit to take 2 arrays as arguments....
Like this you keep both arrayX and arrayY intact, if you need it...

How to store two arrays as a key-value pair in one object in Javascript?

You can use Array.prototype.reduce():

var array1 = ['name', 'lastname'];var array2 = ['john','doe'];var data = array1.reduce((acc, value, i) => (acc[value] = array2[i], acc), {});console.log(data);

Combine two normal arrays to create key:value pair array

I believe you are asking to create an object (associative array) if so

var arr1 = ['one','two','three'];

var arr2 = ['1','2','3'];

var combo_obj = {};

arr1.forEach(function(element, index) {

combo_obj[element] = arr2[index];
});

console.log(combo_obj);

here is its output

{ one: '1', two: '2', three: '3' }

merge two arrays (keys and values) into an object

var r = {},
i,
keys = ['one', 'two', 'three'],
values = ['a', 'b', 'c'];

for (let i = 0; i < keys.length; i++) {
r[keys[i]] = values[i];
}

console.log(r);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Combine the values of two arrays into object

It's one of the ways how to achieve it. You can use Array#forEach function to iterate over every element from array1. Then, create empty object and set specified properties - in your case: meta and value. Then - assign elements to it and just push it into the arr variable.