0

I have an array of objects as follows:

const data = [
  {
    'name': 'A',
    'v0': true,
    'values': [
      { 'v': true, 'k': 1 },
      { 'v': false, 'k': 2 },
      { 'v': true, 'k': 3 },
      { 'v': false, 'k': 4 }
    ]'
  },
  ...
]

the function below extracts the params name, v0 and values from the data objects and is used to render a row of a table by creating an array of cells with properties.

v0 is a boolean, and depending on its value, the render function may apply some styling to the cell.

This works fine when the number of values is predetermined, as they can be listed: v0, v1 ...

However, I have an array of values instead, and would like to spread it, while applying a unique key, as shown in v0 below:

const renderBodyRow = ({ name, v0, values }) => ({
  cells: [
    name,
    v0 ? { key: '0', content: v0, color: 'green' } : { key: '0', content: v0, color: 'red' },
    ...values //  need to apply the same, but on the values array

  ]
})

One way is to have values as a array of booleans: values: [true, false, true, false]

This would require the creation of a unique key inside the renderBodyRow function. Is this possible?

Or define the values array as an array of objects with value and key. In which case, how would spread syntax be able to access the properties of those objects?

Any idea on how to achieve either (or both) of these with spread syntax?

Thanks

2
  • could you please share simple input (with v0 and v1) and output of your input? Commented Feb 8, 2017 at 0:03
  • they would be booleans or any simple type Commented Feb 8, 2017 at 1:39

2 Answers 2

4

You will need to use Array.prototype.map in order to convert each value into an object with the desired style. You can make values a boolean array, and then use the index of each value as the unique key.

const data = [{
  name: 'A',
  values: [true, true, false, true, false]
}];

const renderBodyRow = ({ name, values }) => ({
  cells: [
    name,
    ...values.map((val, index) => ({
      key: String(index),
      content: val,
      color: val ? 'green' : 'red'
    }))
  ]
});

console.log(data.map(renderBodyRow));
.as-console-wrapper { max-height: 100% !important; }

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

5 Comments

I guess you need to use String(index + 1) though to match the first key
@Bergi Yeah, though it appeared to be they wanted the output to be 0-indexed.
@Bergi Ah, yes we're on the same page. My code corresponded with the boolean array alternative that the OP mentioned, while yours corresponds with the structure that's in the first code block. Between our two answers, both structures are covered.
@4castle what i seem to not understand here: is values.map applied before the spread? for some reason i thought the spreading was applied first, so did not try to apply map on it.
@Khorkhe No, first all the array elements in the literal are evaluated, then the array is constructed and filled where the spread syntax is taken into account.
3

You seem to be looking for

function renderBodyCell({ k, v }) {
  return {
    key: k,
    content: v,
    color: v ? 'green' : 'red'
  };
}
function renderBodyRow({ name, v0, values }) {
  return {
    cells: [
      name,
      renderBodyCell({ k: '0', v: v0 }),
      ... values.map(renderBodyCell)
    ]
  };
}

3 Comments

what is v0 bound to in renderBodyCell ?
Ooops, I meant v of course. Happens when copy-pasting for refactoring...
@Bergi thanks! This answer and the one below it are similar in notion, accepted the second one

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.