0

I am using the spread syntax in order to get the current object.

const x = [{ port: 3000, typ: "port" }, { port: 4000, typ: "port" }];
const IDs = [3246237348, 738423894, 73824923]
const y = {
  ...x[0],
  CSSID
};

Object {port: 3000, typ: "port", CSSID: Array[3]}
  port: 3000
  typ: "port"
  CSSID: Array[3]
     0: 3246237348
     1: 738423894
     2: 73824923

But I want to use object assign instead of spread syntax, seems easy but I don't know how to get the result:

const ob = Object.assign(Object.assign({}, x[0], Object.assign(CSSID)) );

Object {0: 3246237348, 1: 738423894, 2: 73824923, port: 3000, typ: "port"}
    0: 3246237348
    1: 738423894
    2: 73824923

1 Answer 1

2

Object.assign() copies properties from one or more objects to a single target object. Since CSSID is an array, it copies the array's properties (the items) to the object. Since you want an object with the property CSSID, set it as a property of the target object, or one of the sources:

CSSID should be a property of an object:

const x = [{ port: 3000, typ: "port" }, { port: 4000, typ: "port" }];
const CSSID = [3246237348, 738423894, 73824923];
const ob = Object.assign({}, x[0], { CSSID }); // or Object.assign({ CSSID }, x[0]);

console.log(ob);

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

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.