编写一个纯函数从内部属性返回一个对象的另一个对象?

时间:2022-09-24 19:01:05

Doing some data transformation exercises and getting stuck. I have an object that I want to transform to look likefrom (starting) -> to (expected ending) output described below. I'm trying to use Array.reduce and Object.assign to keep the output pure. but I just cannot get it to work properly.

做一些数据转换练习并陷入困境。我有一个对象,我想转换为看起来像(起始) - >到(预期结束)输出如下所述。我正在尝试使用Array.reduce和Object.assign来保持输出纯净。但我无法让它正常工作。

/**
 *  from (starting): {topic: {id: 2}, products: {id: 3}}
 *  to (expected ending):   {topic: 2, products: 3}
 */

const starting = {topic: {id: 2}, products: {id: 3}};

const ending = Object.keys(starting).reduce((p, key) => {
  if(!p[key]) p[key] = key;
  return Object.assign(p[key], starting[key].id);  
}, {})

4 个解决方案

#1


7  

You can use reduce, just remember to return the starting object after each iteration.

您可以使用reduce,只需记住在每次迭代后返回起始对象。

function convert(start) {
  return Object.keys(starting).reduce((o, key) => {
      o[key] = start[key].id;
      return o;
    }, {});
}

const starting = {topic: {id: 2}, products: {id: 3}};
console.log(convert(starting));

#2


3  

Try this:

尝试这个:

var starting = {topic: {id: 2}, products: {id: 3}};

var ending = Object.keys(starting).reduce((p, key) => {
  p[key] = starting[key].id;
  return p;
}, {})

This will create a new object, so no need for Object.assign and such to keep the output pure.

这将创建一个新对象,因此不需要Object.assign等来保持输出纯净。

#3


2  

I don't think reduce() is the right tool for this.

我认为reduce()不是正确的工具。

Try iterating through starting's keys using forEach() instead:

尝试使用forEach()迭代启动键:

const starting = {topic: {id: 2}, products: {id: 3}};

const ending = {};
Object.keys(starting).forEach(p => ending[p] = starting[p].id);

console.log(ending);

#4


2  

A pure function solution would require you to return a new object on each iteration:

纯函数解决方案需要您在每次迭代时返回一个新对象:

function convert(start) {
  return Object.keys(starting).reduce((o, key) => 
    Object.assign({}, {
      [key]: start[key].id
    }, o), {});
}

const starting = {topic: {id: 2}, products: {id: 3}};

console.log(convert(starting));

Using object spread makes it a bit cleaner:

使用对象传播使它更清洁:

function convert(start) {
  return Object.keys(starting).reduce((o, key) => ({
    ...o,
    [key]: start[key].id
  }), {});
}

const starting = {topic: {id: 2}, products: {id: 3}};

console.log(convert(starting));

#1


7  

You can use reduce, just remember to return the starting object after each iteration.

您可以使用reduce,只需记住在每次迭代后返回起始对象。

function convert(start) {
  return Object.keys(starting).reduce((o, key) => {
      o[key] = start[key].id;
      return o;
    }, {});
}

const starting = {topic: {id: 2}, products: {id: 3}};
console.log(convert(starting));

#2


3  

Try this:

尝试这个:

var starting = {topic: {id: 2}, products: {id: 3}};

var ending = Object.keys(starting).reduce((p, key) => {
  p[key] = starting[key].id;
  return p;
}, {})

This will create a new object, so no need for Object.assign and such to keep the output pure.

这将创建一个新对象,因此不需要Object.assign等来保持输出纯净。

#3


2  

I don't think reduce() is the right tool for this.

我认为reduce()不是正确的工具。

Try iterating through starting's keys using forEach() instead:

尝试使用forEach()迭代启动键:

const starting = {topic: {id: 2}, products: {id: 3}};

const ending = {};
Object.keys(starting).forEach(p => ending[p] = starting[p].id);

console.log(ending);

#4


2  

A pure function solution would require you to return a new object on each iteration:

纯函数解决方案需要您在每次迭代时返回一个新对象:

function convert(start) {
  return Object.keys(starting).reduce((o, key) => 
    Object.assign({}, {
      [key]: start[key].id
    }, o), {});
}

const starting = {topic: {id: 2}, products: {id: 3}};

console.log(convert(starting));

Using object spread makes it a bit cleaner:

使用对象传播使它更清洁:

function convert(start) {
  return Object.keys(starting).reduce((o, key) => ({
    ...o,
    [key]: start[key].id
  }), {});
}

const starting = {topic: {id: 2}, products: {id: 3}};

console.log(convert(starting));