0

I'm pull data from my data base and converting into to JSON but now I want to place all the JSON data into a single JSON object. I've tried a few methods but I'm not familiar enough with the syntax of javascript and have been unable to find a working solution with proper formatting. I'd like to simply have and object(or two) and continuously add add the new JSON to the older object. I'd prefer not to important any modules for this.

.then(querySnapshot => {
        querySnapshot.forEach(documentSnapshot => {
                myData2 = myData.concat(receiptToDict(documentSnapshot));
                console.log("Found document at", myData2);

            });

});
2
  • Can you post a your JSON? Commented Jul 26, 2018 at 16:47
  • How would looking at the data inside the JSON help? Commented Jul 26, 2018 at 17:31

2 Answers 2

2

You can either

var myData2 = Object.assign({}, myData, receiptToDict(documentSnapshot))

or

var myData2 = { ...myData, ...receiptToDict(documentSnapshot) }

EDIT

If you consider the loop, consider you myData declaration outside of the loop and then:

var myData = {};
   ...
.then(querySnapshot => {
    querySnapshot.forEach(documentSnapshot => {
        myData = Object.assign({}, myData, receiptToDict(documentSnapshot))
        console.log("Found document at", myData);
        });

});

2 EDIT

A object can't have the same property twice, so if you have something like

let myData = {};

let dataArray = [
    {value: 01},
    {value: 02},
    {value: 03},
    {value: 04},
    {value: 05}
]

dataArray.forEach((data) => {
    myData = Object.assign({}, myData, data)
})

console.log(myData);

You will end up replacing always the prop "value"so in this case you should do something like:

 var myData = {};
   ...
.then(querySnapshot => {
    querySnapshot.forEach(documentSnapshot => {
        //I don't know which props you obj has so, I suppose it has a unique id for example
        let doc = receiptToDict(documentSnapshot);
        myData[doc.id] = doc;
        console.log("Found document at", myData);
        });

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

8 Comments

The spread operator (2nd example) is a good solution, but will only work if you're working with ES6 or later.
@AmitB. Yes, that's true... You should use a transpiler like babel in this case, but anyway you can use the Object.assign if it's not the case :)
How could this work given I would be defining the variable myData2 inside the loop. Wouldn’t I be overwriting the variable each pass?
@Blue Ok, you can consider you declared myData outside of the loop and then myData = Object.assign({}, myData, receiptToDict(documentSnapshot))... See the edit
Absolutely, it’s actually declared outside the loop here but I didn’t include it because I’m looking for help in that area as well. I’m not sure how to declare it an empty JSON object. So yea if you can show the best practices for that I’d probably be set.
|
0

myData looks like an Array, so you could do:

.then(querySnapshot => {
        querySnapshot.forEach(documentSnapshot => {
               myData.push(receiptToDict(documentSnapshot));

            });

});

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.