Search code examples
javascriptvelo

Add element to results.items


I'm trying to add a new attribute into an item

for example, if I create the following query

wixData.query("myCollection")
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
      let items = results.items; // see below
    } else {
      // handle case where no matching items found
    }
  } ) ;

I will get this array

/* items:
 *
 * [
 *   {
 *     "_id":          "1234",
 *     "_owner":       "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
 *     "_createdDate": "2017-05-29T08:35:52.344Z",
 *     "_updatedDate": "2017-05-29T08:35:52.344Z",
 *     "title":        "Dr.",
 *     "first_name":   "Jane",
 *     "last_name":    "Doe",
 *     "status":       "active"
 *   },
 *   {
 *     "_id":          "5678",
 *     "_owner":       "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
 *     "_createdDate": "2017-05-25T12:48:56.572Z",
 *     "_updatedDate": "2017-05-29T07:30:15.869Z",
 *     "title":        "Mr.",
 *     "first_name":   "John",
 *     "last_name":    "Doe",
 *     "status":       "active"
 *   }
 * ]
 */

but how to add another key like

"image": "http://image.com"

I tried this

results.items[0].add("image","http://image.com")

but it doen't work


Solution

  • Just

    items[0].image = 'http://image.com';
    

    or

    items[0] = {...items[0], image: 'http://image.com'};