0

This is part of GoJS (diagramming) project and "properties" is itemArray, which is defined inside of one node in diagram.

1) This works (hard coded values):

properties: [
    { "property_name": "Prop1", "property_value": "100"},
    { "property_name": "Prop2", "property_value": "101" },
    { "property_name": "Prop3", "property_value": "102" }
]

2) This doesn't work (from data source):

properties: [
    data.ObjectPropertiesList.forEach(function (item, i) {
        properties.push({ "property_name": item.Item1.toString(), "property_value": item.Item2.toString() });
    })
]

2.1) With surrounding code:

myPalette.model = new go.GraphLinksModel([
    { key: "B", text: "some block", color: "blue" },
    { key: "G", text: "Macro", isGroup: true },
    { category: "circle", key: "Gc", text: "A", color: "black", group: "G", loc: "0 0" },
    {
        category: "table", key: "Ga", group: "G", loc: "60 0",
        properties: [
            data.ObjectPropertiesList.forEach(function (item, i) {
                properties.push({ "property_name": item.Item1.toString(), "property_value": item.Item2.toString() });
            })
        ]
    }
], [
    { from: "Gc", to: "Ga" }
]);
1
  • can you please also post the sorrounding code of the second example you have posted here Commented Jun 21, 2017 at 11:15

3 Answers 3

2

Why pushing data inside var declaration ? just declare your array then push values , see below working snippet

var data= {};
  data.ObjectPropertiesList = [
  {Item1:"Prop1",Item2:"100"},
  {Item1:"Prop2",Item2:"101"},
  {Item1:"Prop3",Item2:"102"},
]

properties = [];

data.ObjectPropertiesList.forEach(function (item, i) {
   properties.push({ "property_name": item.Item1.toString(), "property_value": item.Item2.toString() });
})

console.log(properties);

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

1 Comment

Great :) @tesicg
0

Don't use forEach when you want map:

properties: data.ObjectPropertiesList.map(function (item) {
    return { "property_name": item.Item1.toString(), "property_value": item.Item2.toString() };
})

1 Comment

Because it is a prime example to use map. You have an input array for which you want to modify each element and return the new array. This is exactly what map does, it transforms array items and returns them.
0
var properties = [];
for(var i=0; i < data.length; i++){
    var item = data[i];
    properties.push({ 
        "property_name": item.Item1.toString(), 
        "property_value": item.Item2.toString() 
    })
}

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.