从具有相同键的两个 Javascript 个对象中提取一个公共对象
Extracting a common Object from two Javascript objects with same keys
我有两个具有相同键的 Javascript 对象,但 obj1 中具有某些值的键在 obj2 中为空反之亦然。两个对象中也可以有空键。
obj1 = {
a: 1,
b: 2,
c: ' ',
d: ' ',
e: ' '
}
obj2 = {
a: ' ',
b: ' ',
c: 3,
d: 5,
e: ' '
}
我想要一个组合对象,其中包含具有来自两个对象的值的键,如果两个键都是空的,它只是将其放置为空
Expected result:
result = {
a: 1,
b: 2,
c: 3,
d: 5,
e: ' '
}
我试过使用扩展运算符,但第二个对象总是优先于第一个对象:> {...obj1, ...obj2}
您必须构建一个通用的组合对象,然后遍历每个其他对象,如果不为空则添加值
obj1 = {
a: 1,
b: 2,
c: ' ',
d: ' ',
e: ' '
}
obj2 = {
a: ' ',
b: ' ',
c: 3,
d: 5,
e: ' '
}
// Combine the two to get all keys into the combined object
var combined = { ...obj1, ...obj2 };
// Set each value in the combined object to a single spaced string
for (let key in combined) {
combined[key] = ' ';
}
// Loop through each item in obj1 and add to combined if a single spaced string
for (let key in obj1) {
if (obj1[key] !== ' ') combined[key] = obj1[key];
}
// Loop through each item in obj2 and add to combined if a single spaced string
for (let key in obj2) {
if (obj2[key] !== ' ') combined[key] = obj2[key];
}
console.log(combined)
您可以利用 Object.keys
方法和 reduce
方法来创建结果对象
let obj1 = {
a: 1,
b: 2,
c: ' ',
d: ' ',
e: ' '
}
let obj2 = {
a: ' ',
b: ' ',
c: 3,
d: 5,
e: ' '
}
let result = Object.keys(obj1).reduce((acc, curr) => {
val = obj1[curr] != " " ? obj1[curr] : obj2[curr];
return { ...acc,
...{
[curr]: val
}
}
}, {})
console.log(result)
Object.fromEntries(Object.entries(obj1).map(e=>([e[0], obj1[e[0]]!=' '?obj1[e[0]]:obj2[e[0]]])))
您可以过滤对象并传播第一个和过滤后的对象。
const
filter = o => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== ' ')),
obj1 = { a: 1, b: 2, c: ' ', d: ' ', e: ' ' },
obj2 = { a: ' ', b: ' ', c: 3, d: 5, e: ' ' },
result = { ...obj1, ...filter(obj2) };
console.log(result);
var obj1 = {
a: 1,
b: 2,
c: ' ',
d: ' ',
e: ' '
}
var obj2 = {
a: ' ',
b: ' ',
c: 3,
d: 5,
e: ' '
}
/* in case your objects are not same length */
var longObj = obj1;
var shortObj = obj2;
if(Object.keys(obj1).length < Object.keys(obj2).length) {
longObj = obj2;
shortObj = obj1;
}
for(let k in shortObj) {
longObj[k] = longObj[k] !== ' ' ? longObj[k] : shortObj[k];
}
console.log(longObj);
我有两个具有相同键的 Javascript 对象,但 obj1 中具有某些值的键在 obj2 中为空反之亦然。两个对象中也可以有空键。
obj1 = {
a: 1,
b: 2,
c: ' ',
d: ' ',
e: ' '
}
obj2 = {
a: ' ',
b: ' ',
c: 3,
d: 5,
e: ' '
}
我想要一个组合对象,其中包含具有来自两个对象的值的键,如果两个键都是空的,它只是将其放置为空
Expected result:
result = {
a: 1,
b: 2,
c: 3,
d: 5,
e: ' '
}
我试过使用扩展运算符,但第二个对象总是优先于第一个对象:> {...obj1, ...obj2}
您必须构建一个通用的组合对象,然后遍历每个其他对象,如果不为空则添加值
obj1 = {
a: 1,
b: 2,
c: ' ',
d: ' ',
e: ' '
}
obj2 = {
a: ' ',
b: ' ',
c: 3,
d: 5,
e: ' '
}
// Combine the two to get all keys into the combined object
var combined = { ...obj1, ...obj2 };
// Set each value in the combined object to a single spaced string
for (let key in combined) {
combined[key] = ' ';
}
// Loop through each item in obj1 and add to combined if a single spaced string
for (let key in obj1) {
if (obj1[key] !== ' ') combined[key] = obj1[key];
}
// Loop through each item in obj2 and add to combined if a single spaced string
for (let key in obj2) {
if (obj2[key] !== ' ') combined[key] = obj2[key];
}
console.log(combined)
您可以利用 Object.keys
方法和 reduce
方法来创建结果对象
let obj1 = {
a: 1,
b: 2,
c: ' ',
d: ' ',
e: ' '
}
let obj2 = {
a: ' ',
b: ' ',
c: 3,
d: 5,
e: ' '
}
let result = Object.keys(obj1).reduce((acc, curr) => {
val = obj1[curr] != " " ? obj1[curr] : obj2[curr];
return { ...acc,
...{
[curr]: val
}
}
}, {})
console.log(result)
Object.fromEntries(Object.entries(obj1).map(e=>([e[0], obj1[e[0]]!=' '?obj1[e[0]]:obj2[e[0]]])))
您可以过滤对象并传播第一个和过滤后的对象。
const
filter = o => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== ' ')),
obj1 = { a: 1, b: 2, c: ' ', d: ' ', e: ' ' },
obj2 = { a: ' ', b: ' ', c: 3, d: 5, e: ' ' },
result = { ...obj1, ...filter(obj2) };
console.log(result);
var obj1 = {
a: 1,
b: 2,
c: ' ',
d: ' ',
e: ' '
}
var obj2 = {
a: ' ',
b: ' ',
c: 3,
d: 5,
e: ' '
}
/* in case your objects are not same length */
var longObj = obj1;
var shortObj = obj2;
if(Object.keys(obj1).length < Object.keys(obj2).length) {
longObj = obj2;
shortObj = obj1;
}
for(let k in shortObj) {
longObj[k] = longObj[k] !== ' ' ? longObj[k] : shortObj[k];
}
console.log(longObj);