简体   繁体   English

我想要以下格式的数据

[英]I want data in following format

I am working with some API & I managed to format the data in this order:我正在使用一些 API 并且我设法按以下顺序格式化数据:
Data is in this format数据是这种格式

const data = [
    [
        {
            "point":
            [
                "3",
                "4"
            ],
        },
        {
            "point":
            [
                "5",
                "6"
            ],
        },
        {
            "point":
            [
                "7",
                "8"
            ],
        },
    ],
    [
        {
            "point":
            [
                "9",
                "10"
            ],
        },
        {
            "point":
            [
                "11",
                "12"
            ],
        },
        {
            "point":
            [
                "13",
                "14"
            ],
        },
        {
            "point":
            [
                "15",
                "16"
            ],
        },
    ]
];

From the above data, I want to extract point properties in this order.从上面的数据中,我想按这个顺序提取点属性。
Output is required as: Output 是必需的:

[
    {
        series: {
            data: [[3,4],[5,6],[7,8]]
        }
    },
    {
        series: {
            data: [[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[15,16]]
        }
    },
]

As I proceeded to work, I got to know I am in need of help.当我开始工作时,我知道我需要帮助。 As I am starter in this field, I would like to ask for some help.由于我是这个领域的初学者,我想寻求一些帮助。

I have tried doing:我试过做:

const required = data.map((_, index) => {
  return {
    series: {
      data: data.slice(0, index + 1).map(i => [i[0].point[0], i[0].point[1]])
    }
  }
})

I am able to get first element of each array.我能够获得每个数组的第一个元素。 I could not figure out to include all elements.我想不出包括所有元素。 It is because I have used i[0] .这是因为我使用过i[0] I am not able loop due to lack of my experience.由于缺乏经验,我无法循环。
Output I got: Output 我得到:

[
    {
        series: {
            data: [[3,4]]
        }
    },
    {
        series: {
            data: [[3,4],[9,10]]
        }
    },
]

Other answers presented here appear to present the series array as one of strings.此处提供的其他答案似乎将系列数组表示为字符串之一。 Your required output clearly shows them as numbers, not the strings that would represent them.您所需的 output 清楚地将它们显示为数字,而不是代表它们的字符串。 The following code performs the conversion.以下代码执行转换。

 "use strict"; window.addEventListener('load', onLoaded, false) function onLoaded(evt) { var result = data.map(func); console.log(result); function func(el, index, collection) { let tmpResult = { series: { data: [] } }; el.forEach(pointFunc); function pointFunc(pointObj) { tmpResult.series.data.push(pointObj.point.map(el => parseFloat(el))); } return tmpResult; } } const data = [ [{ "point": [ "3", "4" ], }, { "point": [ "5", "6" ], }, { "point": [ "7", "8" ], }, ], [{ "point": [ "9", "10" ], }, { "point": [ "11", "12" ], }, { "point": [ "13", "14" ], }, { "point": [ "15", "16" ], }, ] ];

Use index, like pass index instead of 0.. i[index]使用索引,例如通过索引而不是 0.. i[index]

let required = data.map(series => ({
    'series': {
        'data': series.map(i => [parseInt(i.point[0]), parseInt(i.point[1])]) // parseInt to convert the strings into numbers
    }
}))

// we use this to add [[3, 4], ...] to the second series [[9, 10], ...].
// To be exactly: it makes that every series includes the previous ones.
required.forEach((_, index) => {
    if (index == 0) return 
    
    required[index].series.data = [...required[index - 1].series.data, ...required[index].series.data]
})

I suppose you need map here:我想你在这里需要map

 var data = [ [ { "point": [ "3", "4" ], }, { "point": [ "5", "6" ], }, { "point": [ "7", "8" ], }, ], [ { "point": [ "9", "10" ], }, { "point": [ "11", "12" ], }, { "point": [ "13", "14" ], }, { "point": [ "15", "16" ], }]]; var result = data.map(k=>k.map(l=>l.point.map(Number))).map((k, i, self)=>[...(self[i-1]??[]), ...k]).map(k=>({series:{data:k}})); console.log(result);

Use map , flatMap and Object.values will simplify.使用mapflatMapObject.values将简化。

 const convert = (data) => data.map((arr) => ({ series: { data: arr.flatMap(Object.values) } })); const data = [ [ { "point": [ "3", "4" ], }, { "point": [ "5", "6" ], }, { "point": [ "7", "8" ], }, ], [ { "point": [ "9", "10" ], }, { "point": [ "11", "12" ], }, { "point": [ "13", "14" ], }, { "point": [ "15", "16" ], }, ] ]; console.log(convert(data));

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

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