简体   繁体   中英

How do I convert an object which stores JSON data into an array in Angular?

I currently want to make a machine learning model using tensorflow.js but I'm stuck at a problem.

I'm saving my JSON content in a local object , but to use it in a machine learning model I need to convert it into an array.

I have an object named guru that has my json array of objects, but to use it in tensorflow.js I need an array like this:

weather [ [1,2,3],[3,2,1],...[] ];

This is my angular component code:

export class DisplayjasonComponent implements OnInit {
    public guru: {}; //local object

    constructor(private http: HttpClient) {
        var obj;
        this.getJSON().subscribe(data => obj = data, error => console.log(error));
    }

    linearModel: tf.Sequential;
    predection: any;

    ngOnInit() {
        this.getJSON().subscribe(data => {
            this.guru = data; //saving json into local object
        });
        this.trainModel();
    }

    private _url: string = "/assets/myjson.json";
    public getJSON(): Observable<any> {
        return this.http.get(this._url);
    }

    async trainModel() {
        this.linearModel = tf.sequential();
        this.linearModel.add(tf.layers.dense({ units: 5, inputShape: [3] }));
        this.linearModel.add(tf.layers.dense({ units: 2 }));
        this.linearModel.compile({ loss: 'meanSquaredError', optimizer: 'sgd' });
        const xs = tf.tensor2d([
            [1, 2, 3], //this is where I need to use the array
            [3, 2, 1]
        ]);
        const ys = tf.tensor2d([
            [1, 0],
            [0, 1]
        ]);
        await this.linearModel.fit(xs, ys)
        const p = this.linearModel.predict(tf.tensor2d([[1, 2, 3]])) as any;
        this.predection = Array.from(p.dataSync());
        console.log(this.predection);
    }
}

This is my JSON File:

{
    "weather": [
        {
            "temprature": 23,
            "precipitation": 2,
            "humidity": 57,
            "weather": 0
        },
        {
            "temprature": 20,
            "precipitation": 100,
            "humidity": 87,
            "weather": 1
        },
        {
            "temprature": 32,
            "precipitation": 5,
            "humidity": 70,
            "weather": 0
        },
        {
            "temprature": 18,
            "precipitation": 87,
            "humidity": 93,
            "weather": 1
        },
        {
            "temprature": 28,
            "precipitation": 0,
            "humidity": 37,
            "weather": 0
        },
        {
            "temprature": 13,
            "precipitation": 94,
            "humidity": 93,
            "weather": 1
        },
        {
            "temprature": 25,
            "precipitation": 4,
            "humidity": 43,
            "weather": 0
        },
        {
            "temprature": 20,
            "precipitation": 68,
            "humidity": 98,
            "weather": 1
        },
        {
            "temprature": 26,
            "precipitation": 0,
            "humidity": 9,
            "weather": 0
        },
        {
            "temprature": 13,
            "precipitation": 100,
            "humidity": 98,
            "weather": 1
        }
    ]
}

I'm not entirely sure what you want the output data to look like but give this a go.

You can actually do this in a single line:

data.weather.map(Object.values);

The .map function is used to transform an array, and the Object.values function will get the value of each field in the object.

 var data = { "weather": [ { "temprature": 23, "precipitation": 2, "humidity": 57, "weather": 0 }, { "temprature": 20, "precipitation": 100, "humidity": 87, "weather": 1 }, { "temprature": 32, "precipitation": 5, "humidity": 70, "weather": 0 }, { "temprature": 18, "precipitation": 87, "humidity": 93, "weather": 1 }, { "temprature": 28, "precipitation": 0, "humidity": 37, "weather": 0 }, { "temprature": 13, "precipitation": 94, "humidity": 93, "weather": 1 }, { "temprature": 25, "precipitation": 4, "humidity": 43, "weather": 0 }, { "temprature": 20, "precipitation": 68, "humidity": 98, "weather": 1 }, { "temprature": 26, "precipitation": 0, "humidity": 9, "weather": 0 }, { "temprature": 13, "precipitation": 100, "humidity": 98, "weather": 1 } ] }; const result = data.weather.map(Object.values); console.log(result); 

I think the below function should format the json according to your needs.

function formatJson(json) {
    let weather = [];
    json.weather.forEach((obj) => {
        let tempArray = Object.values(obj);
        weather.push(tempArray);
    });
    return weather;
}

with json as per below

let json = {
    "weather": [{
        "temprature": 23,
        "precipitation": 2,
        "humidity": 57,
        "weather": 0
    },
    {
        "temprature": 20,
        "precipitation": 100,
        "humidity": 87,
        "weather": 1
    },
    {
        "temprature": 32,
        "precipitation": 5,
        "humidity": 70,
        "weather": 0
    },
    {
        "temprature": 18,
        "precipitation": 87,
        "humidity": 93,
        "weather": 1
    },
    {
        "temprature": 28,
        "precipitation": 0,
        "humidity": 37,
        "weather": 0
    },
    {
        "temprature": 13,
        "precipitation": 94,
        "humidity": 93,
        "weather": 1
    },
    {
        "temprature": 25,
        "precipitation": 4,
        "humidity": 43,
        "weather": 0
    },
    {
        "temprature": 20,
        "precipitation": 68,
        "humidity": 98,
        "weather": 1
    },
    {
        "temprature": 26,
        "precipitation": 0,
        "humidity": 9,
        "weather": 0
    },
    {
        "temprature": 13,
        "precipitation": 100,
        "humidity": 98,
        "weather": 1
    }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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