Javascript 按公用键/索引合并数组项和对象值
我有这样一个数组:Javascript 按公用键/索引合并数组项和对象值,javascript,Javascript,我有这样一个数组: let a = [ ['A', 'B'], // 1 in the object ['C'], // 2 in the object ]; let b = { 5:{ 1: "i was send to earth. i was send to her.", 2: "to mars", 3: { reference: "to moon.",
let a = [
['A', 'B'], // 1 in the object
['C'], // 2 in the object
];
let b = {
5:{
1: "i was send to earth. i was send to her.",
2: "to mars",
3: { reference: "to moon.", expectSt: "to mars. i love it." },
},
};
我有一个这样的物体:
let a = [
['A', 'B'], // 1 in the object
['C'], // 2 in the object
];
let b = {
5:{
1: "i was send to earth. i was send to her.",
2: "to mars",
3: { reference: "to moon.", expectSt: "to mars. i love it." },
},
};
如您所见,对象中有两种模式。图案像1和2,图案像3
我只想将1中的句子添加到let a
内的第一个数组中,将2中的句子添加到let a
内的第二个数组中,依此类推
如果模式是3,那么我只想添加expectSt
和ignorereference
结果应该是:
let a = [
['A', 'B', 'i was send to earth', 'i was send to her'], // 1 in the object
['C', 'to mars'], // 2 in the object
['to mars', 'i love it'], // there is a 3 i the object so we added this
];
我已经尝试了很多,但我想我需要一只手来解决这个问题。let a=[
let a = [
['A', 'B'], // 1 in the object
['C'], // 2 in the object
];
let b = {
5:{
1: "i was send to earth. i was send to her.",
2: "to mars",
3: { reference: "to moon.", expectSt: "to mars. i love it." },
},
};
Object.values(b).forEach(element => {
Object.keys(element).forEach(e => {
console.log(element[e]);
if(e === '1') a[0].push(...element[e].split('.').filter(a => a !== '').map(a => a.trim()));
else if(e === '2') a[1].push(...element[e].split('.').filter(a => a !== '').map(a => a.trim()));
else if(e === '3') {
if(a.length < 3) a.push([]);
a[2].push(...element[e].expectSt.split('.').filter(a => a !== '').map(a => a.trim()))
}
})
});
console.log(a);
对象中的['A','B'],//1
['C'],//对象中的2
];
设b={
5:{
1:“我被送到了地球,我被送到了她那里。”,
2:“去火星”,
3:{参考资料:“去月球”,预期:“去火星,我爱它。”},
},
};
Object.values(b.forEach)(元素=>{
Object.keys(元素).forEach(e=>{
控制台日志(元素[e]);
if(e=='1')a[0]。推送(…元素[e]。拆分('.')).filter(a=>a!=='').map(a=>a.trim());
如果(e=='2')a[1]。推送(…元素[e]。拆分('.'))。过滤器(a=>a!=='')。映射(a=>a.trim());
如果(e=='3'){
如果(a.长度<3)a.推动([]);
a[2].push(…元素[e].expectSt.split('.').filter(a=>a!='').map(a=>a.trim())
}
})
});
控制台日志(a);
像这样简单的东西对你有用吗
设a=[['a','B'],['C'],],
b={5:{1:“我被送到了地球,我被送到了她那里。”,2:“到了火星”,3:{reference:“到了月球。”,expectSt:“到了火星,我爱它。”},},}
对象
.值(b[5])
.forEach((值,键)=>
a[钥匙]=[
…(a[键]| |[]),
…(value.expectSt | | value)
.toLowerCase()
.替换(/\.$/,'')
.split(“.”)
]
)
console.log(a)
.as console wrapper{min height:100%;}
对象值的数量是否总是大于数组项的数量?是否要对进行变异
或在另一个变量中返回合并数组是可以的?否,对象是完全灵活的,3的模式可以在任何地方,对象也可以是空的…更新现有的a
是首选。我不能像我在问题的期望结果中解释的那样分隔句子?我用它分隔句子函数getRawArray(str){return str.toLowerCase().split(“.”.map(s=>s.replace(/\,?,).trim()).filter(s=>s);}
No,我的意思是:[“A”,“B”,“我被送到地球”,“我被送到她那里”],