0

My end goal is an array something like the one below:

let nodes_data =  [
    {"id": "A", "depth": 1, "x":50, "y":100},
    {"id": "B", "depth": 2, "x":150, "y":200},
    {"id": "C", "depth": 2, "x":250, "y":200},
    {"id": "D", "depth": 3, "x":350, "y":300},
] 

However, I am only starting with an id and depth, and I would like to calculate the x and y separately.

So, given the starting source array of:

let nodes_data =  [
    {"id": "A", "depth": 1},
    {"id": "B", "depth": 2},
    {"id": "C", "depth": 2},
    {"id": "D", "depth": 3},
]

I tried to do a for loop to add to the array:

 function presetCoordinates(data){
 let nodes = [];
  for ( let i = 0; i< nodes_data.length; i++) {
    y = data[i].depth*100;
    x = (i*100) + 50;

    nodes.push(x, y)
  }
  return nodes;
}

let nodes_with_coords = presetCoordinates(nodes_data)
console.log(nodes_with_coords) 

Where nodes_with_coords is my "goal" array.

but I'm getting some really strange results. Any thoughts on what I'm missing here? I think maybe I'm over-complicating this.

2
  • Use professional language please... Commented Nov 19, 2018 at 5:53
  • What is the current result? Commented Nov 19, 2018 at 5:53

4 Answers 4

1

You don't include the original object:

function presetCoordinates(data) {
  let nodes = [];
  for (let i = 0; i < nodes_data.length; i++) {
    y = data[i].depth * 100;
    x = (i * 100) + 50;
    
    // include the contents of the original node using spread
    nodes.push({ ...data[i], x, y }) 
  }
  return nodes;
}

let nodes_data =  [
    {"id": "A", "depth": 1},
    {"id": "B", "depth": 2},
    {"id": "C", "depth": 2},
    {"id": "D", "depth": 3},
]

let nodes_with_coords = presetCoordinates(nodes_data)
console.log(nodes_with_coords)

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

Comments

0

You can use "Array.map" for this like below.

What map does is, it loops over every element in "nodes_data" and returns new object/array/{whatever} you need, in your case new object with more data.

let nodes_data =  [
    {"id": "A", "depth": 1},
    {"id": "B", "depth": 2},
    {"id": "C", "depth": 2},
    {"id": "D", "depth": 3},
]

let res = nodes_data.map((d, i) => ({...d, x: i * 100 + 50, y: d.depth * 100 }))

console.log(res)

Comments

0

You could also use reduce() to get the required result.

Demo

const nodes_data =  [
    {"id": "A", "depth": 1},
    {"id": "B", "depth": 2},
    {"id": "C", "depth": 2},
    {"id": "D", "depth": 3},
];

let i = 0;

let result = nodes_data.reduce((r, o) => {
  let x = i + 50,
    y = i + 100;
  i = y;
  return [...r, Object.assign({},o,{x,y})];
}, []);

console.log(result);
.as-console-wrapper {max-height: 100% !important;top: 0;}

Comments

0

use Array.map it simply straight forward

function presetCoordinates(arr){
    return arr.map(function(data, idx){

        data.x = (idx * 100) + 50; 
        data.y = data.depth * 100; 
        return data;        
    });
}

var nodes_data =  [
    {"id": "A", "depth": 1},
    {"id": "B", "depth": 2},
    {"id": "C", "depth": 2},
    {"id": "D", "depth": 3},
];

let nodes_with_coords = presetCoordinates(nodes_data)
console.log(nodes_with_coords) 

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.