简体   繁体   English

想要更改复杂的 Json 结构,我需要在 Javascript 中更改它的数组

[英]Want to change complex Json structure which I need change it array in Javascript

I am new to javascript and I am facing issue with complex structure with I need to break into array.我是 javascript 新手,我面临着复杂结构的问题,我需要进入数组。

my structure is我的结构是

   [
    {
        "Key": "ProducePRINKA0370001",
        "Record": {
            "docType": "Produce",
            "PRODUCEID": "PRINKA0370001"
        }
    },
    {
        "Key": "ProducePRINKA0370038",
        "Record": {
            "docType": "Produce",
            "PRODUCEID": "PRINKA0370038"
        }
    },
    {
        "Key": "ProducePRINKA0370050",
        "Record": {
            "docType": "Produce",
            "PRODUCEID": "PRINKA0370050"
          
        }
    }
]

Which I need to break into array and its should look below我需要分解成数组,它应该在下面看

 [
    {
        "docType": "Produce",
        "PRODUCEID": "PRINKA0370051"
    },
    
        {
        "docType": "Produce",
        "PRODUCEID": "PRINKA0370038"
    }
    
    
]

I have tried all the way however couldn't do it.我已经尝试了所有的方式,但无法做到。 Please help me on this请帮我解决这个问题

thanks in advance提前致谢

You could do it easily using map() as follows -您可以使用map()轻松完成,如下所示 -

 const myArr = [{ "Key": "ProducePRINKA0370001", "Record": { "docType": "Produce", "PRODUCEID": "PRINKA0370001" } }, { "Key": "ProducePRINKA0370038", "Record": { "docType": "Produce", "PRODUCEID": "PRINKA0370038" } }, { "Key": "ProducePRINKA0370050", "Record": { "docType": "Produce", "PRODUCEID": "PRINKA0370050" } } ]; const modifiedArr = myArr.map((el) => { return el.Record; }) console.log(modifiedArr)

The above code just maps over your initial array ( myArr in this case) and just returns the Record part from each of the objects in the myArr array.上面的代码只是映射到您的初始数组(在本例中为myArr ),并且只从myArr数组中的每个对象返回Record部分。 Finally what map() built-in function of JS does is that it returns an array consisting of all elements which you return in the function provided inside map .最后,JS 的map()内置函数所做的是返回一个数组,该数组由您在map内部提供的函数中返回的所有元素组成。

You can use map function to get the result you want.您可以使用 map 函数来获得您想要的结果。

var result = source.map((item) => item["Record"]);

console.log(result);

Here source is the array of the source array.这里的source是源数组的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM