我试图迭代这个块的代码显示表中的数据。我想要相等长度的对象数组
因此,需要为各自的键填充未定义的值,以使对象数组统一(相同长度)
原始Json
[
{
"toolName": "Alteryx",
"contacts": [
{
"contactPerson": "James clear",
"email": "james@google.com"
},
{
"contactPerson": "Paul Unger",
"email": "paulunger@twitter.com"
}
]
},
{
"toolName": "Processes",
"contacts": [
{
"contactPerson": "naomi Unger",
"email": "naomiunger@twitter.com"
}
]
},
{
"toolName": "Alteryx Server",
"contacts": [
{
"contactPerson": "Avinash",
"email": "avinash@meta.com"
},
{
"contactPerson": "Sowmia",
"email": "sowmia@energy.com"
}
]
}
]
期望json
[
{
"toolName": "Alteryx",
"contacts": [
{
"contactPerson": "James clear",
"email": "james@google.com"
},
{
"contactPerson": "Paul Unger",
"email": "paulunger@twitter.com"
}
]
},
{
"toolName": "Processes",
"contacts": [
{
"contactPerson": "naomi Unger",
"email": "naomiunger@twitter.com"
},
{
"contactPerson": null,
"email": null
}
]
},
{
"toolName": "Alteryx Server",
"contacts": [
{
"contactPerson": "Avinash",
"email": "avinash@meta.com"
},
{
"contactPerson": "Sowmia",
"email": "sowmia@energy.com"
}
]
}
]
试过了,但不工作
let max = 0;
const masterArray = res?.data?.tools?.map((obj) => {
obj.contacts.forEach((ele, ind) => {
if(max <= ind){
max = ind;
}
for(let i = 0 ; i< max; i++){
if (ele !== undefined) return ele;
return { ...ele,
contactPerson: '',
email: '',
}
}
});
});
如何填充空/未定义的值来处理错误。
只需要获得一次max
function fillMissingContacts(arr) {
const max = arr.reduce((max, el) =>
Math.max(max, el.contacts.length), 0);
return arr.map(el => {
const contacts = [...el.contacts];
for (let i = contacts.length; i < max; i++) {
contacts[i] = {contactPerson: null, email: null};
}
return {...el, contacts};
});
}
或更改位置。
function fillMissingContacts(arr) {
const max = arr.reduce((max, el) =>
Math.max(max, el.contacts.length), 0);
for (const el of arr) {
for (let i = el.contacts.length; i < max; i++) {
el.contacts[i] = {contactPerson: null, email: null};
}
}
}
你的代码似乎不做你在你的问题中所描述的。如果你对它做一些修改,看起来像这样:
const array = res?.data?.tools;
// Finding max count of contacts
const max = array ? Math.max(...array.map?.(obj => obj.contacts.length)) : 0;
// Filling master array
const masterArray = (array || []).map(obj => {
const emptyContact = () => ({ contactPerson: null, email: null });
// creating contact array; max - contacts.length is the lengths of missing contacts; emptyContact will be called that many times
const contacts = [ ...obj.contacts, ...Array.from({ length: max - obj.contacts.length }, emptyContact)];
// Creating new object with new contacts, so we do not overwrite the original
return { ...obj, contacts };
});
var s = [
{
"toolName": "Alteryx",
"contacts": [
{
"contactPerson": "James clear",
"email": "james@google.com"
},
{
"contactPerson": "Paul Unger",
"email": "paulunger@twitter.com"
}
]
},
{
"toolName": "Processes",
"contacts": [
{
"contactPerson": "naomi Unger",
"email": "naomiunger@twitter.com"
},
{
"contactPerson": null,
"email": null
}
]
},
{
"toolName": "Alteryx Server",
"contacts": [
{
"contactPerson": "Avinash",
"email": "avinash@meta.com"
},
{
"contactPerson": "Sowmia",
"email": "sowmia@energy.com"
}
]
}
]
const handleNullContacts = (obj) => {
obj.map(e=>e?.contacts.map(contact=>{
if (!contact?.contactPerson)
contact["contactPerson"] = '';
if (!contact?.email)
contact["email"] = ''
}))
return obj;
}
s = handleNullContacts(s)
console.log(s)
我使用映射迭代数组,然后另一个映射迭代联系人每当我找到联系人字段undefined
||null
替换为空字符串''
PS:如果我误解了,请让我知道,这样我就可以更新答案