0

I spent more time on this than I would like to admit. I have trouble constructing an object filled with an array.

I would like my data to look like this:

items={
    {
     '2012-05-22': [{text: 'item 1 - any js object'}],
     '2012-05-23': [{text: 'item 2 - any js object'}],
     '2012-05-24': [],
     '2012-05-25': [{text: 'item 3 - any js object'},{text: 'any js object'}],
    }
}

I am making a database call and the data I receive looks like this:

Object {start: "08:00:00", end: "09:00:00", full_name: "Tomomi", date: "2017-06-08", Barber_id: "1"…}

The data I am interested in is the full_name value and the date value.

This is what I have attempted:

let  newItems = {};
axios.post(endpoint, {lookup: day.dateString}).then((customerData) => {
    customerData.data.forEach((val,key)=>{
        newItems = {[val.date]:[]};
        newItems[val.date].push({name:val.full_name});
        console.log(newItems); 
    })
}

It looks like this:

Object {2017-06-08: Array(1)}
2017-06-08
:
Array(1)

This is very close, but the problem is that my code is overwriting my data. I am trying to create this dynamically:

'2012-05-25': [{text: 'item 3 - any js object'},{text: 'any js object'}],

So that each date can have many users. Hopefully, this makes sense.

Thanks for any help.

8
  • 1
    First of, why items = {{}} ? Should be like items = {} Commented Jun 9, 2017 at 20:17
  • How do you call .forEach() on an object without an error? What is customerData.data, an array or an object? Commented Jun 9, 2017 at 20:19
  • Also, from the result you're looking for - seems you're not actually interested at all in full_name Commented Jun 9, 2017 at 20:19
  • I am trying to push the value of the customer into the object. with this line:newItems[val.date].push({name:val.full_name}) Commented Jun 9, 2017 at 20:25
  • @guest271314 it's an array from mysql database. Commented Jun 9, 2017 at 20:26

3 Answers 3

2

The function expression you pass to forEach has this as the first line:

newItems = {[val.date]:[]};

This resets the newItems object to an object with one date:name pair. You really want something more like:

newItems[val.date]?newItems[val.date].push({name:val.full_name}):newItems[val.date]=[];
Sign up to request clarification or add additional context in comments.

1 Comment

This was very helpful. Thank you.
1

var byDate = {}; // Object to store received data by-date

function addIntoByDate( obj ) {
  byDate[obj.date] = byDate[obj.date] || [];
  byDate[obj.date].push( obj );
}

// Simulate adding server data one by one
addIntoByDate( {date: "2017-06-08", full_name: "Cat", text:"Foo!!"}  ); // < SAME DATE
addIntoByDate( {date: "2016-05-23", full_name: "Dog", text:"Bar"}    );
addIntoByDate( {date: "2017-06-08", full_name: "Bug", text:"Baz..."} ); // < SAME DATE

// test
console.dir(byDate);

Comments

1

You can use object destructuring, computed property and Object.assign()

const newItems = {};

const data = [
               {
                 start: "08:00:00"
               , end: "09:00:00"
               , full_name: "Tomomi"
               , date: "2017-06-08"
               , Barber_id: "1"
               }
             ];

data.forEach(({date, full_name}) => 
  Object.assign(newItems, {[date]: [{/* text: */ full_name}]}));
  
console.log(newItems);

1 Comment

I totally forgot about Object.assign this was helpful, but Roko's answer worked a bit better for me.

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.