1

i am returning a structure of an object of folders and the contents of the json files contained in them. this is how it's looks like.

{
  DailyTask: [ { title: 'Chores', body: 'Wash the roof and sweep the bed' } ],
  monday: [{ title: 'madrid', body: 'Wash the roof and sweep the bed' }  ],
}

i have a problem when a folder has more than two files, because i can't find a way to append to the array of either monday or DailyTask

i tried doing concat or push but they wouldn't work at first because the object property is undefined so i did the first assignment would be done via square brackets and subsequent ones will be done via push or unshift i.e x is the json file

if (sum[key] == undefined) {
            sum[key] = x;
        } else {
            sum[key].unshift(x);
        }

gives me this

{
  DailyTask: [ { title: 'Chores', body: 'Wash the roof and sweep the bed' } ],
  monday: [
    [ [Object] ],
    { title: 'madrid', body: 'Wash the roof and sweep the bed' }
  ]
}

it shows as [[Object]], how do i make the actual contents of the object show.

5
  • Ciao, so you want create a unique array that contains both objects { title: 'Chores', body: 'Wash the roof and sweep the bed' } and { title: 'madrid', body: 'Wash the roof and sweep the bed' } correct? Commented Aug 9, 2020 at 13:44
  • Show us result of before and after. Commented Aug 9, 2020 at 13:44
  • @Sascha the first code block and the last code block are the results Commented Aug 9, 2020 at 13:48
  • @GiovanniEsposito i want to add more of the format { title: 'XXXXX', body: 'Do something xxxxxxx' } Commented Aug 9, 2020 at 13:49
  • yourObject.monday.push({title:"foo"}); should work. Provide a jsfiddle of your issue. Commented Aug 9, 2020 at 13:53

2 Answers 2

1

From the logic you are presenting x equals [ {...} ]. that is why it works for

sum[key] = x;
// outputs:
// title: [ { ... } ]

But fails on the other: title: [ [{...}], {...} ]

try this:

if (sum[key] == undefined) {
  sum[key] = [x[0]] ; // or simply leave it x;
} else {
  sum[key].unshift(x[0]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

you misunderstood me, i am pushing an object of format { title: 'XXX', body: 'bla bla bla bla' } not title: [ [{...}], {...} ]
but that's exactly the output you are getting. [[ [Object] ],{ title: 'madrid', body: 'Wash the roof and sweep the bed' }]. my offered solution doesn't work still?
0
if(sum[key]==undefined) sum[key]=[x];
else sum[key].push(x);

2 Comments

this doesn't work , the subsequent pushed objects show as [ [Object] ]
Doesn't make sense to me. What is x? console.log it and see if it's something like this: {"title":"something", "body":"something else"}.

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.