Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

js替换数组中的某个键值对

var arr = [
    {
      "fdId": "1ech3enh02snwcw1q9fd0q2jv026k1hb",
      "actionType": "agree",
      "triggerTime": 48,
    },
    {
      "fdId": "1ecp2el9j35ow2dw34t42dd1klcdea1s",
      "actionType": "forward",
      "triggerTime": 27,
    }
  ]

生成一个新数组,把fdId替换成id
应该怎么转换


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var arr = [
    {
      "fdId": "1ech3enh02snwcw1q9fd0q2jv026k1hb",
      "actionType": "agree",
      "triggerTime": 48,
    },
    {
      "fdId": "1ecp2el9j35ow2dw34t42dd1klcdea1s",
      "actionType": "forward",
      "triggerTime": 27,
    }
];
var newArr = [];

arr.forEach(({ fdId, ...rest }) => newArr.push({ id: fdId, ...rest }));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...