map object to array

Maps an object to an array of objects using the provided mapping function.

const listify = (obj, mapFn) =>
  Object.entries(obj).reduce((acc, [key, value]) => {
    acc.push(mapFn(key, value));
    return acc;
  }, []);
const people = { John: { age: 42 }, Adam: { age: 39 } };
listify(people, (key, value) => ({ name: key, ...value }));
// [ { name: 'John', age: 42 }, { name: 'Adam', age: 39 } ]

Acho que você gosta

Origin blog.csdn.net/liuhao9999/article/details/129856448
Recomendado
Clasificación