我想从JSON创建一个新的array.问题是我不知道我将得到的JSON的 struct .它可能是任何东西.但我知道JSON的密钥,它将包含新数组所需的所有数据.例如,假设键是‘data’,它是包含我需要的所有数据的array.除此之外,我还必须在带有键‘data’的JSON数组中查找‘CustomerName’、‘CustomerAddress’和‘CustomerAge’.我将使用这些键的值创建新的array.因此,新数组So应该如下所示:

[
    {
         name: '', // it will be customerName from the JSON
         address: '', // it will be customerAddress from the JSON
         age: '', // it will be customerAge from the JSON
     },
     {
         // more objects
     }
]

请注意,CustomerName、CustomerAddress和CustomerAge可以存在于JSON中的"data"数组中的嵌套数组或对象中.所以我得go 找他们.

我为它创建了一个递归函数,但它返回一个空array.

这就是我try 过的:

function extractDataFromJSON(json, key) {
    const result = [];

    function findData(obj) {
        if (Array.isArray(obj)) {
            for (const item of obj) {
                findData(item);
            }
        } else if (obj && typeof obj === 'object') {
            if (key in obj) {
                if (Array.isArray(obj[key])) {
                    for (const item of obj[key]) {
                        findData(item);
                    }
                } else if (
                    'customerName' in obj[key] &&
                    'customerAddress' in obj[key] &&
                    'customerAge' in obj[key]
                ) {
                    const { customerName, customerAddress, customerAge } = obj[key];
                    result.push({
                        name: customerName || '',
                        address: customerAddress || '',
                        age: customerAge || '',
                    });
                }
            } else {
                for (const prop in obj) {
                    if (obj.hasOwnProperty(prop) && typeof obj[prop] === 'object') {
                        findData(obj[prop]);
                    }
                }
            }
        }
    }

    findData(json);
    return result;
}

const jsonData1 = {
    data: [
        {
            customerName: 'John Doe',
            customerAddress: '123 Main St',
            customerAge: 30,
        },
        {
            customerName: 'Jane Smith',
            customerAddress: '456 Maple Ave',
            customerAge: 25,
        },
        // ... other data ...
    ],
};

// const jsonData2 = [
//     {
//          data: [
//                  {
//                      customerName: 'John Doe',
//                      customerAddress: '123 Main St',
//                      customerAge: 30,
//                 },
//                 {
//                      customerName: 'Jane Smith',
//                      customerAddress: '456 Maple Ave',
//                      customerAge: 25,
//                 },
//                 // ... other data ...
//          ],
//     }
// ];

// const jsonData3 = {
//     data: [
//         {
//             customerName: 'John Doe',
//             customerAge: 30,
//             address: {
//                 customerAddress: '123 Main St',
//             }
//         },
//         {
//             customerName: 'Jane Smith',
//             customerAge: 30,
//             address: {
//                 customerAddress: '456 Maple Ave',
//             }
//         },
//     ],
// };



const newArray = extractDataFromJSON(jsonData1, 'data');
console.log(newArray);

我还添加了另外两个对象作为测试,以判断我的函数是否也适用于这些类型的 struct .

任何帮助都将不胜感激.

更新

const jsonData1 = {
    data: [
        {
            customerName: 'John Doe',
            customerAddress: '123 Main St',
            customerAge: 30,
        },
        {
            customerName: 'Jane Smith',
            customerAddress: '456 Maple Ave',
            customerAge: 25,
        },
        // ... other data ...
    ],
    data2: [
        {
            customerName: 'John Doe2',
            customerAddress: '123 Mainsss St',
            customerAge: 30,
        },
        {
            customerName: 'Jane Smith3',
            customerAddress: '456 Mapleaaa Ave',
            customerAge: 25,
        },
        // ... other data ...
    ],
};

推荐答案

您可以使用相同的函数进行递归,只需将结果数组传递给它. 这里有一些收集嵌套对象属性的技巧--我们提供acc(属性累加器)参数来收集子代中的属性:

IMPORTANT REMARK个 行动需要收集所有字段:

                    'customerName' in obj[key] &&
                    'customerAddress' in obj[key] &&
                    'customerAge' in obj[key]

因此不会收集此 case ,因为该字段名重复:

//             customerName: 'Jane Smith',
//             customerAddress: '456 Maple Ave',
//             address: {
//                 customerAddress: '456 Maple Ave',
//             }

我想这是个错误.此外,该代码不会try 查找重复项,而是使用第一个找到的属性.如果数据中包含重复项就很奇怪了.

function extractDataFromJSON(obj, fields, result = [], acc = null) {

    if (Array.isArray(obj)) {
        for (const item of obj) {
            extractDataFromJSON(item, fields, result, acc);
        }
    } else if (obj && typeof obj === 'object') {

        const keys = Object.keys(obj).filter(key => fields[key]);
        const fieldsLen = Object.keys(fields).length;

        if (keys.length) {

            const item = acc || {};

            for (const key in obj) {
                if (fields[key]) {
                    item[fields[key]] = obj[key] || '';
                    continue;
                }
                // check the property if the object is incomplete
                keys.length < fieldsLen && extractDataFromJSON(obj[key], fields, null, item);
            }
            // push only complete items and if the result array is present 
            // (not the collecting properties mode)
            if(result && Object.keys(item).length === fieldsLen){
                result.push(item);
            }

        } else {
            for (const key in obj) {
                extractDataFromJSON(obj[key], fields, result, acc);
            }
        }

    }
    return result;

}

const jsonData1 = {
    data: [
        {
            customerName: 'John Doe',
            customerAddress: '123 Main St',
            customerAge: 30,
        },
        {
            customerName: 'Jane Smith',
            customerAddress: '456 Maple Ave',
            customerAge: 25,
        },
        // ... other data ...
    ],
    data2: [
        {
            customerName: 'John Doe2',
            customerAddress: '123 Mainsss St',
            customerAge: 30,
        },
        {
            customerName: 'Jane Smith3',
            customerAddress: '456 Mapleaaa Ave',
            customerAge: 25,
        },
        // ... other data ...
    ],
};

const jsonData3 = [{
    differentKey: [
        {
            customerName: 'John Doe',
            customerAge: 30,
            iAmDummy: [{ dummyArray: [null, undefined, '', false, new Date] }],
            customerAddress: '123 Main St'
        },
        {
            customerName: 'Jane Smith',
            iAmDummy: [{ dummyArray: [null, undefined, '', false, new Date] }],
            lookDeeper: {customerAge: 40},
            address: [{
                customerAddress: '456 Maple Ave',
            }]
        },
    ],
}];


const newArray1 = extractDataFromJSON(jsonData1, {customerName: 'name', customerAddress: 'address', customerAge: 'age'});
console.log(newArray1);

const newArray3 = extractDataFromJSON(jsonData3, {customerName: 'name', customerAddress: 'address', customerAge: 'age'});
console.log(newArray3);

Javascript相关问答推荐

导入图像与内联包含它们NextJS

HTML/JavaScript函数未执行

如何从JavaScript中的公共方法调用私有方法?

使用NgDeliverentOutlet和动态内容投影的Angular 渲染组件

如何在加载的元数据上使用juserc和await中获得同步负载?

如何使用Paged.js仅渲染特定页面

如何从对象嵌套数组的第二级对象中过滤出键

我的glb文件没有加载到我的three.js文件中

Next.js Next/Image图像隐含性有任何类型-如何修复?

如何修复循环HTML元素附加函数中的问题?

有条件的悲剧

角色 map 集/spritebook动画,用户输入不停止在键上相位器3

如何在模块层面提供服务?

使用GraphQL查询Uniswap的ETH价格

覆盖加载器页面避免对页面上的元素进行操作

DOM不自动更新,尽管运行倒计时TS,JS

我不知道如何纠正这一点.

如果我的列有条件,我如何呈现图标?

未找到用于 Select 器的元素:in( puppeteer 师错误)

固定动态、self 调整的优先级队列