0

How can I push an object for example with {x, y, z} values into an array of another object with only {x, y} values?

Vector2 V2List = []

Vector3 V3 = new Vector3(1,2,3);

//.. code here

V2List = [{1, 2}]

This is just an example, on my real work, my object has more than 10 of props.

3 Answers 3

2

If I understood correctly:

let v2template = {
  one: 1,
  two: 2
};

let v3 = {
  one: 6,
  two: 7,
  three: 8
};

let v2array = [{
    one: 1,
    two: 2
  },
  {
    one: 3,
    two: 4
  },
  {
    one: 5,
    two: 6
  }
];

v2array.push(Object.keys(v2template).reduce(function(prev, item) {
  prev[item] = v3[item];
  return prev;
}, {}));

console.log(v2array);

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

Comments

2

const V3 = function (x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

const v3 = new V3(1,2,3);

function insert(v3) {
    const arr = JSON.parse(JSON.stringify(v3));
    const V2 = [];
    Object.entries(arr).map((item) => {
        (item[0] === 'x' || item[0] === 'y') && V2.push({ [item[0]]: item[1] })
    })
    return V2;
}

console.log(insert(v3));

Comments

1

You can add Vector3 to a Vector2 list if you initialize with size.

var V2List = new Vector2[2];
Vector3 V3 = new Vector3(1,2,3);
V2List[0] = V3;
V2List[1] = {1,2};

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.