【发布时间】:2022-11-18 19:07:33
【问题描述】:
已经尝试了一段时间我想做的事情:
我有两个具有相同键名和不同值的不同对象,我需要创建一个新数组,该数组将包含一个具有两个条目的新对象,两个对象的值具有相同的键。
enter code here
OBJ1{ keyNAME : 'lalala', toto: 'bbbb', tata: 'blablabla' }
OBJ2{ keyNAME : 18, toto: 7, tata: null }
// here something that i imagine could look similar to:
let newObjectKeys = ['title', 'value' ]
function createMyNewArray()=> {
let newArray = []
Use somehow OBJ1 and OBJ2, check the keys and create new array using
newObjectKeys
i think it might use Object.keys method but all i have tried i don't get to the
result i need so i'm defo missing something
}
return newArray;
console.log("new Array", newArray)
输出看起来像:
const newArray =[
{
string: "lalala",
value: 18
},
{
string: 'bbbb',
value: 7,
},
{
string: 'blablabla'
value: null
},
....
];
然后我可以像这样在我的正面使用它:
{newArray.map((item)=> return(
<div>
p {item.string}
p {item.value}
</div>
))}
谢谢你
【问题讨论】:
-
如果 OBJ2 没有 OBJ1 的相应属性,应该发生什么。比如如果 OBJ1 有 {foo: 1} 而 OBJ2 根本没有“foo”。
标签: javascript reactjs object