在Javascript中循环Json的最佳方法

时间:2017-10-13 12:09:02

标签: javascript json

所以我有这个设置json

[{
        "name": "Business",
        "skip": "0",
        "pos": "1"
    },
    {
        "name": "contact",
        "skip": "1",
        "pos": "3"
    },
    {
        "name": "UKSite",
        "skip": "0",
        "pos": "2"
    }
]

然后我有我的数据json

[{
    "name": "contact",
    "hideShow": true,
    "data": {
        "con_title": "Sri",
        "con_fName": "Jhon",
        "con_lName": "Snow",
        "emails": ["maheshwar@hsjsk.com", ""],
        "phones": ["8867116216", ""]
    },
    "repeatable": false
}, {
    "name": "UKAddress",
    "hideShow": true,
    "data": {
        "addAddress": [{
            "add_bilding_num": "BUILDING NAME/NUMBER",
            "add_street_name": "STREET",
            "add_town": "TOWN",
            "add_county": "COUNTY",
            "add_pc": "POST CODE",
            "add_type": "TYPE"
        }, {
            "add_bilding_num": "BUILDING NAME/NUMBER",
            "add_street_name": "STREET",
            "add_town": "TOWN",
            "add_county": "COUNTY",
            "add_pc": "POST CODE",
            "add_type": "TYPE"
        }]
    },
    "repeatable": false
}, {
    "name": "Business",
    "hideShow": true,
    "data": {
        "biss_name": "Nano Corporation"
    },
    "repeatable": false
}, {
    "name": "contact",
    "hideShow": true,
    "data": {
        "con_title": "Mr",
        "con_fName": "Roshan",
        "con_lName": "Aslam",
        "emails": ["roshan@throughbit.com"],
        "phones": ["8867116216"]
    },
    "repeatable": true
}, {
    "name": "UKSite",
    "hideShow": true,
    "data": {
        "site_name": "naonocrporation.co.uk",
        "external_rff": "fsfsfs",
        "con_title": "Mr",
        "con_fName": "Kash",
        "con_lName": "Sangh",
        "emails": ["kash@youremail.com"],
        "phones": ["8867116216"],
        "addAddress": [{
            "add_bilding_num": "",
            "add_street_name": "",
            "add_town": "",
            "add_county": "",
            "add_pc": "",
            "add_type": ""
        }],
        "ElecMeter": [{
            "distrib_id": "DISTRIBUTOR ID",
            "pc": "PROFILE CLASS",
            "mtc": "METER TIMESWITCH CODE",
            "llf": "LINE LOSS FACTOR",
            "mpc": "MPAN CORE",
            "eac": "EAC",
            "c_supplr": "CURRENT SUPPLIER",
            "c_date": "CURRENT END DATE",
            "select": true
        }, {
            "distrib_id": "",
            "pc": "",
            "mtc": "",
            "llf": "",
            "mpc": "",
            "eac": "",
            "c_supplr": "",
            "c_date": ""
        }],
        "GasMeter": [{
            "mprn": "MPRN",
            "aq": "AQ",
            "g_c_supplr": "CURRENT SUPPLIER",
            "g_c_date": "CURRENT END DATE"
        }]
    },
    "repeatable": true
}]

我必须循环数据json并使用设置json的信息需要创建一个新的Json

例如,

我的新json应该看起来像这样

[{
        "name": "Business",
        "hideShow": true,
        "data": {
            "biss_name": "Nano Corporation"
        },
        "repeatable": false
    },
    {
        "name": "contact",
        "hideShow": true,
        "data": {
            "con_title": "Mr",
            "con_fName": "Roshan",
            "con_lName": "Aslam",
            "emails": [
                "roshan@throughbit.com"
            ],
            "phones": [
                "8867116216"
            ]
        },
        "repeatable": true
    },
    {
        "name": "UKAddress",
        "hideShow": true,
        "data": {
            "addAddress": [{
                    "add_bilding_num": "BUILDING NAME/NUMBER",
                    "add_street_name": "STREET",
                    "add_town": "TOWN",
                    "add_county": "COUNTY",
                    "add_pc": "POST CODE",
                    "add_type": "TYPE"
                },
                {
                    "add_bilding_num": "BUILDING NAME/NUMBER",
                    "add_street_name": "STREET",
                    "add_town": "TOWN",
                    "add_county": "COUNTY",
                    "add_pc": "POST CODE",
                    "add_type": "TYPE"
                }
            ]
        },
        "repeatable": false
    }
]

你可以观察,术语skip:1这意味着跳过第一个,并添加休息术语pos:1,它告诉新对象在新数组中的位置。

任何帮助都会很棒

更新1

我想出了这个片段

ar finalArray = [] for(var i = 0; i < b.length; i++){ var counter = 0; for(var j = 0; j < a.length; j++){ if(b[i].name == a[j].name){ if(counter < b[i].skip){ counter++; continue; } finalArray.push(a[j]) } } }

由于

1 个答案:

答案 0 :(得分:0)

当您处理一组对象并且您希望在迭代期间根据条件修改每个对象时,我建议您使用forEach

你有这里的文档和示例: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/map