Pizhev Racing
Pizhev Racing

Reputation: 486

How to change name on array in JSON using node.js and express

I have a API and to change the name on index of array from 0 to some name.

They are two array's under ardaforecast and I want to change the first array with name data and second array with name details.

    // Create express app
var express = require("express")
var app = express()
var mysql = require('mysql')
var express = require("express")
var cors = require('cors')

app.use(cors())

// Server port
var HTTP_PORT = 3000

var pool = mysql.createPool({
  connectionLimit: 10,
  host: '192.168.0.1',
  user: 'UName',
  port: '3333',
  password: 'Pass',
  database: 'dbName'
});

var ardaforecast = '';


app.route('/')
  .get(function (req, res) {
    // omitted
    res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');
    const id = req.query.id;

    pool.query(`CALL Get_Alert_levels_Station(${id});`, function (error, result) {
      if (error)
      return res.status(500).json({ error: "Грешна заявка. Опитай отново !"})
      ardaforecast = result;
      res.json({ ardaforecast })
    });
  });

// Start server
app.listen(HTTP_PORT, () => {
  console.log("Server running on port %PORT%".replace("%PORT%", HTTP_PORT))
});

pool.on('error', function (err) {
  console.log(err.code); // 'ER_BAD_DB_ERROR'
});

app.use(function (req, res) {
  res.status(404);
});

Upvotes: 0

Views: 187

Answers (1)

Apoorva Chikara
Apoorva Chikara

Reputation: 8773

A simple way of doing this will be creating a list of keys you want to add and iterate through the array ardaforecast:

const data = {"ardaforecast":[[{"Dt":"2021-07-22T23:00:00.000Z","AL":1},{"Dt":"2021-07-23T02:00:00.000Z","AL":1},{"Dt":"2021-07-23T05:00:00.000Z","AL":1},{"Dt":"2021-07-23T08:00:00.000Z","AL":1},{"Dt":"2021-07-23T11:00:00.000Z","AL":1},{"Dt":"2021-07-23T14:00:00.000Z","AL":1},{"Dt":"2021-07-23T17:00:00.000Z","AL":1},{"Dt":"2021-07-23T20:00:00.000Z","AL":1},{"Dt":"2021-07-23T23:00:00.000Z","AL":1},{"Dt":"2021-07-24T02:00:00.000Z","AL":1},{"Dt":"2021-07-24T05:00:00.000Z","AL":1},{"Dt":"2021-07-24T08:00:00.000Z","AL":1},{"Dt":"2021-07-24T11:00:00.000Z","AL":1},{"Dt":"2021-07-24T14:00:00.000Z","AL":1},{"Dt":"2021-07-24T17:00:00.000Z","AL":1},{"Dt":"2021-07-24T20:00:00.000Z","AL":1},{"Dt":"2021-07-24T23:00:00.000Z","AL":1},{"Dt":"2021-07-25T02:00:00.000Z","AL":1},{"Dt":"2021-07-25T05:00:00.000Z","AL":1},{"Dt":"2021-07-25T08:00:00.000Z","AL":1},{"Dt":"2021-07-25T11:00:00.000Z","AL":1},{"Dt":"2021-07-25T14:00:00.000Z","AL":1},{"Dt":"2021-07-25T17:00:00.000Z","AL":1},{"Dt":"2021-07-25T20:00:00.000Z","AL":1},{"Dt":"2021-07-25T23:00:00.000Z","AL":1},{"Dt":"2021-07-26T02:00:00.000Z","AL":1},{"Dt":"2021-07-26T05:00:00.000Z","AL":1},{"Dt":"2021-07-26T08:00:00.000Z","AL":1},{"Dt":"2021-07-26T11:00:00.000Z","AL":1},{"Dt":"2021-07-26T14:00:00.000Z","AL":1},{"Dt":"2021-07-26T17:00:00.000Z","AL":1},{"Dt":"2021-07-26T20:00:00.000Z","AL":1},{"Dt":"2021-07-26T23:00:00.000Z","AL":1},{"Dt":"2021-07-27T02:00:00.000Z","AL":1},{"Dt":"2021-07-27T05:00:00.000Z","AL":1}],{"fieldCount":0,"affectedRows":0,"insertId":0,"serverStatus":34,"warningCount":0,"message":"","protocol41":true,"changedRows":0}]};

const newData = {};
const keys = ['data', 'details']; // you can change it or add new keys 
data.ardaforecast.forEach((el, i) => {
      newData[keys[i]] = el;
});

console.log(newData);

Upvotes: 1

Related Questions