javascript 将具有数组的对象变换为数组[已关闭]

pxy2qtax  于 2023-01-16  发布在  Java
关注(0)|答案(6)|浏览(122)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

三年前关闭了。
Improve this question
我有下面的数据结构:

const data = {
  "firstName": "A",
  "lastName": "B",
  "address": [{
    "country": "France",
    "city": "Paris"
  },
    {
      "country": "Italy",
      "city": "Rome"
    }
  ],
};

使用Ramda,我想将其转换为:

const result = [
  {
    "firstName": "A",
    "lastName": "B",
    "address": {
      "country": "France",
      "city": "Paris"
    },
  },
  {
    "firstName": "A",
    "lastName": "B",
    "address": {
      "country": "Italy",
      "city": "Rome"
    },
  },
];
erhoui1w

erhoui1w1#

可以使用converge函数来派生prop address,然后将其与列表中每个地址的main对象连接:

/**
 * R.pick could be replaced with R.omit
 * to let you black list properties:
 * R.omit(['address']); https://ramdajs.com/docs/#omit
**/
const createByAddress = R.converge(R.map, [
  R.pipe(R.pick(['firstName', 'lastName']), R.flip(R.assoc('address'))),
  R.prop('address'),
]);

const data = {
  "firstName": "A",
  "lastName": "B",
  "address": [{
    "country": "France",
    "city": "Paris"
    },
    {
      "country": "Italy",
      "city": "Rome"
    }
  ],
};

console.log(createByAddress(data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>
utugiqy6

utugiqy62#

已锁定19小时。此时正在解析disputes about this answer’s content。当前不接受新的交互。

我的问题是为什么要“和Ramda一起”?我是Ramda的创始人之一,也是一个超级粉丝,但它只是一个工具,除非这是Ramda的一个学习练习,否则似乎没有必要用它来解决这个问题。
我会这样做,使用现代JS技术:

const transform = ({address, ...rest}) => 
  address .map (a => ({...rest, address: a}))

const data = {firstName: "A", lastName: "B", address: [{country: "France", city: "Paris"}, {country: "Italy", city: "Rome"}]}

console .log (
  transform (data)
)
4zcjmb1e

4zcjmb1e3#

我不确定这是否会对您有所帮助,但是如果您希望基于adress生成多个对象,这可能会有所帮助

const obj = {
  firstName: "a",
  lastName: "b",
  adresses: [{
    country: "France",
    city: "Paris"
  }, {
    country: "Italy",
    city: "Rome"
  }]
};

adressAmount = obj.adresses.length;

const adressObjects = [];

for (let i = 0; i < adressAmount; i++) {
  const {
    adresses,
    ...objWithoutAdresses
  } = obj;
  objWithoutAdresses.adress = obj.adresses[i];
  adressObjects.push(objWithoutAdresses);
}

console.log(adressObjects);
g52tjvyc

g52tjvyc4#

我觉得这个很简单很短。

const data = {
  "firstName": "A",
  "lastName": "B",
  "address": [{
    "country": "France",
    "city": "Paris"
  },
    {
      "country": "Italy",
      "city": "Rome"
    }
  ],
};



let requiredData = data.address.map(element=>{
return {...data,address:element}
})

console.log(requiredData);
kxxlusnw

kxxlusnw5#

1)创建空字典
2)for循环数组并将每个数组的索引作为值存储在字典中

6rqinv9w

6rqinv9w6#

您可以根据需要迭代地址数组并创建对象

let obj = {
  "firstName": "A",
  "lastName": "B",
  "address": [{
      "country": "France",
      "city": "Paris"
    },
    {
      "country": "Italy",
      "city": "Rome"
    }
  ]
}


let newData = obj.address.map(function(item) {

  return {
    firstName: obj.firstName,
    lastName: obj.lastName,
    address: {
      country: item.country,
      city: item.city
    }
  }


});
console.log(newData)

相关问题