const obj = {
aaaName: {
name: 'aaaName',
formDataValue: {
name: '111',
type: 'list',
itemType: 'string',
formDataValue: ['a', 'b', 'c']
}
},
ccc: {
name: 'ccc',
formDataValue: {
name: 'ccc',
type: 'list',
itemType: 'string',
formDataValue: {
name: '111',
type: 'list',
itemType: 'string',
formDataValue: '111'
}
}
},
bbbName: {
name: 'bbbName',
type: 'string',
ab: {a: 'b', b: 'c'},
formDataValue: [
{
name: '222',
type: 'list',
itemType: 'string',
formDataValue: ['a', 'b', 'c']
},
{
name: '333',
type: 'list',
itemType: 'string',
formDataValue: 'abc'
},
]
}
}
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function isObject(value) {
return Object.prototype.toString.call(value) === '[object Object]';
}
function isArray(value) {
return Object.prototype.toString.call(value) === '[object Array]';
}
function formatData(obj) {
if (!isObject(obj)) return;
Object.keys(obj).forEach(item => {
if (item !== 'name' && item !== 'formDataValue') {
delete (obj[item]);
}
if (item === 'formDataValue') {
if (isObject(obj[item])) {
formatData(obj[item])
}
if (isArray(obj[item])) {
obj[item].forEach(_item => {
if (isObject(_item)) {
formatData(_item)
}
})
}
}
})
}
function run(obj) {
Object.values(obj).forEach(item => {
if (isObject(item)) {
formatData(item);
}
})
}
run(obj)
function setData(obj) {
Object.keys(obj).forEach(key => {
if (isObject(obj[key])) {
const tempObj = {};
tempObj[obj[key].name] = obj[key].formDataValue;
obj[key] = tempObj;
}
if (isArray(obj[key])) {
obj[key].forEach(_item => {
if (isObject(_item)) {
}
})
}
})
}
function run2(obj) {
const temp = {};
Object.values(obj).forEach(item => {
temp[item.name] = item.formDataValue;
})
return temp;
}
个人随记1
最新推荐文章于 2024-11-05 22:07:06 发布