Srinath M
Srinath M

Reputation: 43

How to give one object from an array of objects to zingchart?

Consider I have an array of objects with the following structure


var dummyArray = [
{diff: "0.000", totalLength: 3},
{diff: "0.001", totalLength: 3},
{diff: "0.002", totalLength: 4},
{diff: "0.003", totalLength: 2},
{diff: "0.004", totalLength: 1},
{diff: "0.005", totalLength: 3},
{diff: "0.012", totalLength: 1},
{diff: "0.016", totalLength: 1},
{diff: "0.023", totalLength: 1},
{diff: "0.044", totalLength: 1},
{diff: "0.111", totalLength: 1},
{diff: "0.156", totalLength: 1}
];

Now I want to give the diff as a parameter to the following zingchart configurtaion.

"scale-x": {
              "values": "1994:2014",
              "max-items": 99,
              "guide": {
                "visible": false
              }
           },

Instead of values "1994:2014" Something like the following way

"scale-x": {
              "values": dummyArray.diff,
              "max-items": 99,
              "guide": {
                "visible": false
              }
           },

Consider datatables API, how we can give directly object(property) name.

Similarly, Is it possible to give something like the above way directly without manipulating the data by any array filters?

Thanks in advance

Upvotes: 0

Views: 56

Answers (1)

Sameer
Sameer

Reputation: 5188

The only way to assign all the values is to use map with toString() like below
dummyArray.map(x => x.diff).toString();

(Working Example)

const dummyArray = [{diff:"0.000",totalLength:3},{diff:"0.001",totalLength:3},{diff:"0.002",totalLength:4},{diff:"0.003",totalLength:2},{diff:"0.004",totalLength:1},{diff:"0.005",totalLength:3},{diff:"0.012",totalLength:1},{diff:"0.016",totalLength:1},{diff:"0.023",totalLength:1},{diff:"0.044",totalLength:1},{diff:"0.111",totalLength:1},{diff:"0.156",totalLength:1}];

const myObj = {
  "values": dummyArray.map(x => x.diff).toString(),
  "max-items": 99,
  "guide": {
    "visible": false
  }
}

console.log(myObj);

In case you have array with list of string then you don't need map, You just do toString()

dummyArray = ["0.000","0.001","0.002","0.003","0.004","0.005","0.012","0.016","0.023","0.044","0.111","0.156"];
dummyArray.toString();

Upvotes: 1

Related Questions