2

I want to duplicate specific objects inside an array.
I have the array1 and I want to get the array2.

Example:

const array1 = [
    { a: String, b: Number, c: [3]}, 
    { a: String, b: Number, c: [12, 13]}, 
    { a: String, b: Number, c: [ 4, 5, 6]}
]

array2 = [
    { a: String, b: Number, c: [3]},
    { a: String, b: Number, c: [12]}, 
    { a: String, b: Number, c: [13]}, 
    { a: String, b: Number, c: [4]},
    { a: String, b: Number, c: [5]},
    { a: String, b: Number, c: [6]},
]

4
  • Could you try being a bit more specific? Commented Dec 6, 2019 at 19:05
  • 3
    Where is your current attempt? Commented Dec 6, 2019 at 19:07
  • I don't know what is going on, but I am making a mongoose call that returns an array. After I manipulated the array like the 'kind user' did I get all this meta data that seems to be related to mongoose. [ { "$__": { "strictMode": true, "selected": {}, "getters": { }, "_id": "5cd582e8e29b5cd582e8e29b", "populated": { "seller": { ... Commented Dec 9, 2019 at 14:25
  • If I use lean() it works, but I need it populated and I can't make it work with lean() Commented Dec 9, 2019 at 14:26

2 Answers 2

1

Array#reduce may come handy

const array1 = [
    { a: 'String', b: 'Number', c: [3]}, 
    { a: 'String', b: 'Number', c: [12, 13]}, 
    { a: 'String', b: 'Number', c: [ 4, 5, 6]}
];

const r = array1.reduce((s, a) => 
   (s.push(a.c.length > 1 ? a.c.map((t) => ({ ...a, c: [t] })) : a), s), []);
   
   document.write('<pre>' + JSON.stringify(r, null, 2) + '</pre>');

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

3 Comments

ok, this worked with the example that I provided but my problem is different. I don't know what is going on, but I am making a mongoose call that returns an array. After I manipulated the array like you did I get all this meta data that seems to be related to mongoose. [ { "$__": { "strictMode": true, "selected": {}, "getters": { }, "_id": "5cd582e8e29b5cd582e8e29b", "populated": { "seller": { ...
If I use lean() it works, but I need it populated and I can't make it work with lean()
@BrunoKern I guess it would be better to ask a new question related with lean method and mongodb:)
0

      const array1 = [
        { a: String, b: Number, c: [3] },
        { a: String, b: Number, c: [12, 13] },
        { a: String, b: Number, c: [4, 5, 6] }
      ];

      var array2 = [];
      array1.map(obj => {
        var obj2 = {};
        for (var key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (!Array.isArray(obj[key])) {
              obj2[key] = obj[key];
            } else {
              obj[key].map(item => {
                obj2[key] = item;
                array2.push({ ...obj2 });
              });
            } // end if-else
          } // end hasOwnProperty
        } // end for
      });
      console.log(array2);

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.