0

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

phd
  • 82,685
  • 13
  • 120
  • 165
srinath
  • 43
  • 4
  • To assign the value from variable you would do `dummyArray[0].diff` – Sameer May 22 '20 at 10:02
  • @sameer-khan values param accepts an array of values and my requirement is I want to pass all the values wit the object "diff"........... we can do this by passing something like this ```[dummyArray[0].diff, dummyArray[1].diff, dummyArray[2].diff...........] ``` or we can make an array of values of diff object something like this ```let result = objArray.map(a => a.foo)``` and give that as input. But my query was is there any smart way where we can achieve this functionality without manipulating the data – srinath May 22 '20 at 11:29

1 Answers1

1

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();
Sameer
  • 4,758
  • 3
  • 20
  • 41
  • Yes I agree we can get the values by manipulating the data to the required format, but I was checking is there any way that without manipulating the data directly giving the "Property" itself as a parameter – srinath May 23 '20 at 18:20
  • There is no way to manipulate the format of the input data once it is ZingChart. Data pre-processing is a common practice and required for ZingChart. Note: Not all chart types have the same data input structure. – nardecky Jun 17 '20 at 21:43