0

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);
});
4
  • Can you add the data and what is the expected data? I can see the response from API and there is an array ardaforecast? What do you want to do? Commented Jul 22, 2021 at 14:15
  • Inside in ardaforecast array have other two array's. I want to rename it from 0 and 1 to data and details .. Commented Jul 22, 2021 at 14:31
  • You can't change the keys of arrays to string values. You might need object to do, iterate through the array and for both the index add values to the key. Commented Jul 22, 2021 at 14:41
  • Can I get example how ? Commented Jul 22, 2021 at 18:06

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.