0

I want to check every value element inside an Array, check if it contain an Array or not. If found, make all the element inside value Array to become string. Any help would be greatly appreciated.

this.array.filter( tgt => tgt.value).includes(Array()).join('++')

initial array

[{id: 1, value: [{true,false}]},{id: 2, value: "foo"}]

final array

[{id: 1, value: "true++false"},{id: 2, value: "foo"}]
mitchiri_neko
  • 1,735
  • 2
  • 7
  • 12

1 Answers1

1

How about

const initial_array = [{ id: 1, value: [true, false] }, { id: 2, value: "foo" }]

function process(arr) {
    return arr.map(el => {
        return (Array.isArray(el.value)) ? { ...el, value: el.value.join('++') } : el
    })
}

console.log(process(initial_array))

basically we're mapping all the elements of the array individually. If value is an array then replace it using the code you provided, otherwise include it without change. The {...el, value: ___} bit uses the spread operator to replace the value field of the object

Peter Hull
  • 6,683
  • 4
  • 39
  • 48