0

I'm setting up columns in angular-ui-grid like this:

            $scope.columns.push({
                displayName: 'TimeStamp', field: 'x', width: $scope.setGridColumnWidth(), sort: {
                    direction: uiGridConstants.DESC,
                    priority: 1
                }
            });

            for (let i = 0; i < view.trend.getTagsList().length; i++)
                $scope.columns.push({ displayName: view.trend.getTagsList()[i].getTitle(), field: 'y' + view.trend.getTagsList()[i].getId(), width: $scope.setGridColumnWidth() });

So if a trend contains 2 tags I'll get a ui-grid with the columns {x, y{id0}, y{id1} }.

Now I'm trying to bind data using:

            $scope.data = [];
            let taglist = view.trend.getTagsList();

            for (let i = 0; i < taglist.length; i++)
                for (let n of taglist[i].getData())
                    $scope.data.push({ x: n.x, 'y' + taglist[i].getId(): n.y });

But I can't dynamically define an attribute in a json using 'y' + taglist[i].getId() instead it wants a fixed value. Anyone knows a neat way to solve this?

President Camacho
  • 1,860
  • 6
  • 27
  • 44
  • 1
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – adeneo May 17 '16 at 09:45
  • Just as side node, you should save `view.trend.getTagsList()` in a variable. – jcubic May 17 '16 at 09:52
  • @adeneo On the contrary, referring to a simple object of key-value pairs as a "JSON-like object" is nice and descriptive. It is certainly less verbose than "Object instance" or "collection of key-value pairs". – John Weisz May 17 '16 at 09:52
  • 1
    @JohnWhite: On the contrary, it's misleading and perpetuates a misunderstanding that leads to nonsense code and confused thinking (just hang out in the `javascript` tag for a couple of days). It's an "object." That's all. adeneo is quite right to try to fix it wherever he finds it. – T.J. Crowder May 17 '16 at 09:53
  • @T.J.Crowder Indeed it is, assuming involved people are confused between an *"instance of `Object`"* and a *"string containing Javascript object notation"* -- which is certainly not the case with even intermediary-level developers (although I perfectly understand what you are trying to hint at). – John Weisz May 17 '16 at 09:57
  • JSON is text, not object. If you need to get updated JSON string you have adjust object and serialize it to JSON format with JSON.stringify or form new JSON string based on previous value ( adjust string, not recommended). – Andriy Ivaneyko May 17 '16 at 10:03

3 Answers3

3

You want ES6's dynamic properties.

They would allow you to do something like this

$scope.data.push({ x: n.x, ['y' + view.trend.getTagsList()[i].getId()]: n.y });

You can use a ES6-ES5 transpiler to achieve this. I see you have this tagged with typescript. As of Typescript 1.5 the compiler will handle this. For those stuck on ES5, create the object manually.

let obj = { x: n.x };
obj['y' + view.trend.getTagsList()[i].getId()] = n.y;
$scope.data.push(obj);
Paarth
  • 9,687
  • 4
  • 27
  • 36
1

If i understand you correctly, you want to dynamically set a key in your object.

You can do that as follows:

var y_final = 'y' + view.trend.getTagsList()[i].getId();
var obj = {};
obj[y_final] = n.y;
obj[x] = n.x;
$scope.data.push(obj);
Kito
  • 1,375
  • 4
  • 17
  • 37
1

Have you tried initializing first the object? Something like ...

var newObj = {"x": n.x};
eval ('newObj.y'+view.trend.getTagsList()[i].getId()+' = '+ n.y);

A simple test here: https://jsfiddle.net/544vso45/

Marc Compte
  • 4,579
  • 2
  • 16
  • 22