0

I've been trying to copy a nested object properties into another object by using the spread operator. But, I got an error.
So I tried to spread the nested object separately:

try {
  console.log(
    ...{
      autre_panne: { nbr_op_sav: 12, percentage_op_sav: 0 },
      fonctionnement_bluetooth: { nbr_op_sav: 13, percentage_op_sav: 0 },
      memoire_interne: { nbr_op_sav: 15, percentage_op_sav: 0 },
      non_precise: { nbr_op_sav: 15, percentage_op_sav: 0 },
      panne_audio: { nbr_op_sav: 68, percentage_op_sav: 0 },
      piece_aspect_mecanique: { nbr_op_sav: 78, percentage_op_sav: 0 },
      probleme_charge: { nbr_op_sav: 2, percentage_op_sav: 0 },
    }
  );
} catch (error) {
  console.log("Error spreading");
  console.log(error);
}

I get this error:

Error spreading TypeError: Found non-callable @@iterator
at Object. (C:\Users\Avempace\Desktop\Projects\playground\play.js:227:11)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)


PS: Similar questions mentioned in SO, do not answer my question:
1. How to use object spread with nested properties?
2. How to spread nested properties in Object?

1
  • You are spreading the object directly inside console.log() which isn't valid Commented May 29, 2020 at 16:14

2 Answers 2

1

Not sure if this is an issue with the way you test your actual problem, but you need to spread your object into an object - in the console.log call I'm not sure what you want the end result to be but you if you spread into an object it should work:

try {
  console.log(
    {...{
      autre_panne: { nbr_op_sav: 12, percentage_op_sav: 0 },
      fonctionnement_bluetooth: { nbr_op_sav: 13, percentage_op_sav: 0 },
      memoire_interne: { nbr_op_sav: 15, percentage_op_sav: 0 },
      non_precise: { nbr_op_sav: 15, percentage_op_sav: 0 },
      panne_audio: { nbr_op_sav: 68, percentage_op_sav: 0 },
      piece_aspect_mecanique: { nbr_op_sav: 78, percentage_op_sav: 0 },
      probleme_charge: { nbr_op_sav: 2, percentage_op_sav: 0 },
    }}
  );
} catch (error) {
  console.log("Error spreading");
  console.log(error);
}

By "it should work", I mean that you should be able to print the object (that is actually spread into a new object).

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

Comments

0

You can try:

const myData = {<the data>} 
console.log(...Object.entries(myData).flat());

Object.entries converts each key-value pair into arrays within an array. flat() spreads each array into the main array. ...Object destructure the final array.

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.