javascript - JavaScript 函数没有返回正确的数据
问题描述
我有 3 个数据集,它们是对象数组,我想更改text
属性值。
一些数据集具有递归结构。这意味着它具有一个children
属性,该属性是具有相同结构的对象数组。
我做了一个函数,它根据id
. 2nd
但是在or数据集的迭代中面临一个问题3rd
。我正在使用该map
方法进行迭代,检查是否item
具有children
属性并再次调用相同的方法。
为什么在2nd
或3rd
数据集上,函数返回子属性值?
// Dataset 1
const data = [{
id: '1',
pid: null,
children: null,
text: ''
}];
// Dataset 2
const data2 = [{
id: '1',
pid: null,
text: '',
children: [{
id: '1.1',
pid: '1',
children: null,
text: ''
}],
}];
// Dataset 3
const data3 = [{
id: '1',
pid: null,
text: '',
children: [{
id: '1.1',
pid: '1',
children: null,
text: ''
},
{
id: '1.2',
pid: '1',
children: null,
text: ''
}
],
}];
/**
* @param {string} iD
* @param {string} text
* @param {Array.<Object>} data
* @returns {Array.<Object>}
*/
const changeText = (iD, text, data) => {
const newArr = data.map(item => {
if (item.id === iD) {
// Construct new array of object with updated values
return {
...item,
text
};
} else {
if (item.children !== null && item.children.length > 0) {
return changeText(iD, text, item.children)
}
// Wrong id return old data
return item;
}
});
return newArr;
};
// Test for 1st Dataset
console.log('--Dataset 1---');
console.log(changeText('1', 'Foo', data)); // Passed
console.log('--Dataset 1---');
console.log(changeText('1.1', 'Foo', data)); // Passed
// Test for 2nd Dataset
console.log('--Dataset 2---');
console.log(changeText('1.1', 'Foo', data2)); // Value is changed but children array is return
// Test for 3rd Dataset
console.log('--Dataset 3---');
console.log(changeText('1.2', 'Foo', data3)); // Value is changed but children array is return
解决方案
您应该将 item.children 设置为 changeText 递归调用的结果并返回项目本身(在更改之后)。
如果你不想改变 item.children 的值,你应该返回一个更新了 children 属性的新对象(return { children: changeText(..), ... })而不是“原始”项目。
// Dataset 1
const data = [{
id: '1',
pid: null,
children: null,
text: ''
}];
// Dataset 2
const data2 = [{
id: '1',
pid: null,
text: '',
children: [{
id: '1.1',
pid: '1',
children: null,
text: ''
}],
}];
// Dataset 3
const data3 = [{
id: '1',
pid: null,
text: '',
children: [{
id: '1.1',
pid: '1',
children: null,
text: ''
},
{
id: '1.2',
pid: '1',
children: null,
text: ''
}
],
}];
/**
* @param {string} iD
* @param {string} text
* @param {Array.<Object>} data
* @returns {Array.<Object>}
*/
const changeText = (iD, text, data) => {
const newArr = data.map(item => {
if (item.id === iD) {
// Construct new array of object with updated values
return {
...item,
text
};
} else {
if (item.children !== null && item.children.length > 0) {
item.children = changeText(iD, text, item.children);
return item;
}
// Wrong id return old data
return item;
}
});
return newArr;
};
// Test for 1st Dataset
let resultArray = [];
console.log('--Dataset 1---');
console.log(changeText('1', 'Foo', data)); // Passed
console.log('--Dataset 1---');
console.log(changeText('1.1', 'Foo', data)); // Passed
// Test for 2nd Dataset
console.log('--Dataset 2---');
console.log(changeText('1.1', 'Foo', data2)); // Value is changed but subpart return
// Test for 3rd Dataset
console.log('--Dataset 3---');
console.log(changeText('1.2', 'Foo', data3)); // Value is changed but subpart return
推荐阅读
- xml - xmllint 无法读取属性值
- python - TypeError:'float' 对象不可下标(REFPROP)
- sql - 用于跟踪每月账单的 SQL 数据库(初学者)
- mysql - MySQL没有读取整行
- mysql - 我可以选择另一个表中与该表中的 FK 具有相同值的行数吗?
- reactjs - 如何使用 ReactQuery 中的 useQuery 挂钩来更新状态?
- r - 转换字符列,然后使用 r mutate 将其拆分为多个新的布尔列
- c++ - 如何模拟 find_package() 与 add_subdirectory() 一样?(现代 CMake)
- php - 在用户数据脚本中的 EC2 AWS 实例中运行 composer 命令不工作
- python - 最小硬币找零问题 - 回溯