1

I'm not sure about the map syntax.

I've tried to fetch the result score from the original object to be part of a new object.

filteredResults.push(currentResults
  .map(({ item, result }) => ({
    ...item,
    resultScore,
    result: {
      ...result,
      name:
        !Array.isArray(result.name) ?
          result.name
          : result.name
            .filter(({ '@language': lang }) => !negLanguagesParam.includes(lang))
    }
  }))

How do you read this syntax?

It doesn't follow the API syntax.

For each iteminCurrentResult modify ...

What does the ... mean, and how should I use this correctly?

How would you add the resultScore to be next the name in the new object?

Array of original objects:

enter image description here

And I wish it would turn into:

enter image description here

Update

I think these comments explain what I was looking for:

filteredResults.push(currentResults
  .map(({ allOther, resultScore, result }) => ({
    ...allOther,  
    result: {
      ...result, //shallow copy the object
      resultScore,  //add new memebr1
      name: //add new memebr2
        !Array.isArray(result.name) ?
          result.name
          : result.name
            .filter(({ '@language': lang }) => !negLanguagesParam.includes(lang)) //({}) for taking a specific named attribute
    }
  }))
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • 1
    `...` is the [Spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax). In this case, it's used to make a shallow copy of all the key/values from the original `item` into a new object, and add some extra keys (and same for `result`). `({item, result}) =>` is a [Destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). It extracts the `item` and `result` properties from the current element into named parameters you can use. And `=> ({...})` is like `=> { return {...}; }` – blex May 17 '21 at 23:16
  • `...` in console preview or while using debugger means that there is more data and you can load it on click (it prevents loading a lot of data that is not needed). – ulou May 17 '21 at 23:20
  • @blex Thanks, how would you add the `resultScore` to be next the `name` in the new object? – Elad Benda May 17 '21 at 23:26
  • `It extracts the item and result properties from the current element into named parameters you can use` but there is no `item property. No? – Elad Benda May 17 '21 at 23:26
  • Well... not in the screenshots you showed us. Would you mind adding and actual demo input Object & desired output Object **in plain text** in your question, so we can copy them and work with them? – blex May 17 '21 at 23:29

0 Answers0