How can I not overwrite data in the object, but add?

  • 0
    let contentObject = {items: {}}
    for (let key in cleanContent) {
        console.log(cleanContent[key])
        contentObject.items = {
            "name": cleanContent[key].market_hash_name,
            "price": cleanContent[key].price,
            "quality": cleanContent[key].ru_rarity
        }
    }
    // В базу данных, очевидно попадает это
    {
      "items": {
        "name": "Little Green Jade Dragon",
        "price": "58.44",
        "quality": "Rare"
      }
    }


    Is it possible to add data to an object without overwriting it? Or should I do it through arrays
    JavaScript Juliet Spears, Oct 1, 2020

  • 2 Answers
  • 0
    let contentObject = {
    items: []
    }

    cleanContent.forEach(content => contentObject.items.push(content));
    Chase Horton

  • 0
    contentObject.items = {
    ...contentObject.items,
    "name": cleanContent[key].market_hash_name,
    "price": cleanContent[key].price,
    "quality": cleanContent[key].ru_rarity
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!