How to combine data in an object into another array in JavaScript

https://blog.csdn.net/qq_26222859/article/details/70331833

var json1 = [
{"guoshui":[
300000, 500000, 600000, 800000, 1000000, 1200000, 1400000, 1600000, 1800000, 1600000, 1400000, 1200000
]},
{"dishui":[
1100000, 1200100, 1300000, 1100000, 1050000, 1400000, 1200000, 1600000, 1800000, 1200000, 1400000, 1100000
]},
{"heji":[
400000, 1300000, 1200000, 1100000, 1500000, 1400000, 1200000, 1600000, 1700000, 1100000, 1400000, 1400000
]}
]

  

 

 

There are the following objects:

Now I want to integrate it into an array to form the following array:

[{
        name: 'national tax',
        type: 'bar',
       data: [300000, 500000, 600000, 800000, 1000000, 1200000,1400000,1600000,1800000,1600000,1400000,1200000],
markPoint : {
                data : [
                    {type : 'max', name: 'max'},
                    {type : 'min', name: 'minimum'}
                ]
            },
 markLine : {
                data : [
                    {type : 'average', name: 'average'}
                ]
            }
    },
{
        name: 'land tax',
        type: 'bar',
       data: [1100000, 1200100, 1300000, 1100000, 1050000, 1400000,1200000,1600000,1800000,1200000,1400000,1100000],
markPoint : {
                data : [
                    {type : 'max', name: 'max'},
                    {type : 'min', name: 'minimum'}
                ]
            },
 markLine : {
                data : [
                    {type : 'average', name: 'average'}
                ]
            }
    },
{
        name: 'Total',
        type: 'bar',
       data: [400000, 1300000, 1200000, 1100000, 1500000, 1400000,1200000,1600000,1700000,1100000,1400000,1400000],
markPoint : {
                data : [
                    {type : 'max', name: 'max'},
                    {type : 'min', name: 'minimum'}
                ]
            },
 markLine : {
                data : [
                    {type : 'average', name: 'average'}
                ]
            }
    },
]





In JavaScript, an array is actually an object, or an object with array-like properties.

It converts the subscript of the array to a string and uses it as a property. If you use typeof to judge an array, you get

The result is object. The first value of the array automatically gets the property name '0', the second value automatically gets the property name '1', and so on.



Regarding JavaScript's arrays and objects, let me talk about their differences first:

The object is automatically expanded, and the properties of the object can be dynamically added through ".property name".

Because the array is an object, it is also automatically expanded, and the properties of the object can be dynamically added through ".property name" or [property name] or push().


The way to traverse the object is for ..in.. random access, using the property name as the traversal key

The way to traverse the array is to use the for loop to access sequentially, you can directly use i++ as the traversal key. For ..in.. random access is not recommended



back to the previous question

Integrating the data in the object into another array in JavaScript involves the expansion of the array and the traversal of the object and the array.



So it is written as follows:

 var array1 = [{
        name: 'national tax',
        type: 'bar',
markPoint : {
                data : [
                    {type : 'max', name: 'max'},
                    {type : 'min', name: 'minimum'}
                ]
            },
markLine : {
                data : [
                    {type : 'average', name: 'average'}
                ]
            }},
{name: 'land tax',
        type: 'bar',
markPoint : {
                data : [
                    {type : 'max', name: 'max'},
                    {type : 'min', name: 'minimum'}
                ]
            },
markLine : {
                data : [
                    {type : 'average', name: 'average'}
                ]
            }},
{
name: 'Total',
        type: 'bar',
markPoint : {
                data : [
                    {type : 'max', name: 'max'},
                    {type : 'min', name: 'minimum'}
                ]
            },
markLine : {
                data : [
                    {type : 'average', name: 'average'}
                ]
            }
}];


//array1[0].data=json1['guoshui'];
//array1[1].data=json1['dishui'];
//array1[2].data=json1['heji'];

var i = 0;
for(item in json1){
 array1[i++].data=json1[item];
}

    //console.log(json1[item]);
    //array[item]=json1[item];
console.log(array1);

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324815005&siteId=291194637