Merge Keys Array and Values Array into an Object in JavaScript

Merge keys array and values array into an object in JavaScript

Simple JS function would be:

function toObject(names, values) {
var result = {};
for (var i = 0; i < names.length; i++)
result[names[i]] = values[i];
return result;
}

Of course you could also actually implement functions like zip, etc as JS supports higher order types which make these functional-language-isms easy :D

How to merge array of keys and arrays of values into an object?

use this one.

var a = ["F", "M"];
var b = ["female", "male"];
var c = ["fa-female", "fa-male"];

var resultArray = [];
for(var i = 0; i < a.length; i++) {
resultArray [a[i]] = [b[i], c[i]];
}

Merge two array of objects based on a key value compare

You can do something like this:

const users = [
{
p_id: 1,
name: "Peter",
status: "Active",
},
{
p_id: 2,
name: "Kane",
status: "Active",
},
{
p_id: 3,
name: "William",
status: "Inactive",
},
];

const phoneNumbers = [
{ p_id: 1, type: "home", no: "+01 234 5678" },
{ p_id: 1, type: "work", no: "+09 111 2223" },
{ p_id: 2, type: "home", no: "+12 345 6789" },
];

const mergeArrays = (arr1, arr2) => {
return arr1.map((obj) => {
const numbers = arr2.filter((nums) => nums["p_id"] === obj["p_id"]);
if (!numbers.length) {
obj.phone = numbers;
return obj;
}
obj.phone = numbers.map((num) => ({ type: num.type, no: num.no }));
return obj;
});
};

const result = mergeArrays(users, phoneNumbers);
console.log(result);

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; }

Array: Merge keys and values, to call as an object

For each value, map over the keys array to create a tuple of [key, value], then use Object.fromEntries to construct your object.

var keys = ["height", "width"];
var values = [["12px", "24px"],["127px", "284px"]];

var dimensions = values.map(value => (
Object.fromEntries(keys.map((key, index) => ([key, value[index]])))
));

console.log(dimensions);

Merge objects in array based on one key using reduce

Here is a use Array.prototype.reduce to alter the shape of array

let data = [
{sku: "BS-WHITE-1", GB: 15, total: 0, inbound: 0, available: 0},
{sku: "BS-WHITE-1", DE: 4},
{sku: "BS-WHITE-1", ES: 0},
{sku: "BS-WHITE-1", IT: 3},
{sku: "BS-WHITE-1", FR: 0},
{sku: "BS-WHITE-2", GB: 19, total: 40, inbound: 0, available: 40},
{sku: "BS-WHITE-2", DE: 2},
{sku: "BS-WHITE-2", FR: 5},
{sku: "BS-WHITE-2", ES: 3},
{sku: "BS-WHITE-2", IT: 6},
{sku: "BS-WHITE-3", GB: 21, total: 51, inbound: 1, available: 50},
{sku: "BS-WHITE-3", DE: 1},
{sku: "BS-WHITE-3", ES: 1},
{sku: "BS-WHITE-3", IT: 2},
{sku: "BS-WHITE-3", FR: 2},
{sku: "BS-WHITE-4", GB: 43, total: 42, inbound: 8, available: 31},
{sku: "BS-WHITE-4", DE: 8},
{sku: "BS-WHITE-4", FR: 7},
{sku: "BS-WHITE-4", ES: 3},
{sku: "BS-WHITE-4", IT: 17}
];

const result = data.reduce((accumulator, current) => {
let itemIndex = accumulator.findIndex(item => item.sku === current.sku);
if(itemIndex != -1) {
accumulator[itemIndex] = {...accumulator[itemIndex], ...current};
} else {
accumulator = accumulator.concat(current);
}
return accumulator;
}, []);

console.log(result);

Merging objects from array with the same key value

You could use reduce with Object.keys to do that.

var arr = [
{id: 1, tech_id:11, action: 'swim'},
{id: 2, tech_id:11, action: 'run'},
{id: 3, tech_id:22, action: 'climb'},
{id: 4, tech_id:22, action: 'swim'},
{id: 5, tech_id:11, action: 'jump'},
]

let mergeObj = arr.reduce((p, c) => {
const {tech_id, ...otherData} = c;

if (!(tech_id in p)) {
p[tech_id] = {
data: []
}
}

p[tech_id].data.push(otherData)
return p
}, {})

mergeObj = Object.keys(mergeObj).map(key => {
return {
tech_id: key,
data: mergeObj[key].data
}
})

console.log(mergeObj);

Merge objects of Array by key name

I guess this format of result would be better

myArray = {    even: [2,4,6,4,6,8,16,18],     odd: [1,3,5,7,7,9,11,13,15,15,17],     decimals: [3.14, 12.8, 111.1],}

Merge / Combine object arrays into single object

Assuming your example was wrong, and it's actually like below (since what you put in isn't even valid), you can just use Object.values(obj).flat()

var obj = {
"0": [{
"category": "A",
"index": 0,
"property": "Name",
"value": "Bob"
}],
"1": [{
"category": "A",
"index": 1,
"property": "Name",
"value": "Jessica"
}]
}

console.log(Object.values(obj).flat())


Related Topics



Leave a reply



Submit