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.
var array1 = ["Bob", "John", "Dave"], array2 = [1, 2, 3], arr = [];
array1.forEach(function(v,i){ var obj = {}; obj.meta = v; obj.value = array2[i]; arr.push(obj);});
console.log(arr);
Related Topics
If Check Box Checked Disable Other, If Unchecked Enable All in React
Want to Hide Menu by Clicking Outside the Menu Element
Generate and Download CSV File With PHP and Ajax
Prevent User from Typing in Input At Max Value
Get a List of Dates Between Two Dates Using JavaScript
Reload Datatable After Ajax Success
How to Intercept Button Click Inside Uiwebview on Ios
Callback Function Cannot Access Variable Within Parent Function'S Scope
While Looping a Jquery Ajax Call
Prevent Multiple Clicks .Click()
React How to Fix Failed Prop Type - Invalid Prop of Type String Expected Object
Angular:Onclick on HTML Element According to Its Class
Detect If Page Is Load from Back Button
How to Remember the Value of a Variable After Reloading the Page
Invalid Configuration Object in Webpack
Javascript Loop Wait Until API Call Finished