猿问

如何在函数返回中向对象添加多个数组?

for我有一个函数,它接受一个对象作为道具,需要使用循环向它添加数组。我的问题是,它仅在调用时添加单个数组时才有效。如果要添加多个数组,我会收到错误linkLineItems.push is not a function,但我认为.push可以用于将数组添加到对象。


这是功能:


function PrepareSuccessorActivityLinkData(data, existingLinks, linkSetter) {

  for (let [key, value] of Object.entries(data)) {

    let linkLineItems;

    let linkLineItem;

    if (data.activitiesafter[0] != "DEFAULT") {

      for (var i = 0; i < data.activitiesafter.length; i++) {

        linkLineItem = {

          source: data.itemname,

          target: data.activitiesafter[i],

          type: "activity-activity-after"

        };

        if (!linkLineItems) {

          linkLineItems = linkLineItem;

        } else {

          linkLineItems.push(linkLineItem);

        }

      }

    } else {

      continue;

    }

    return linkSetter(linkData => [...existingLinks, linkLineItems]);

  }

}

有关如何将多个数组添加到对象的任何帮助?


existingLinks编辑#1:我尝试向其中添加项目的 or 对象的示例数据


var linksData = [

 {"source": "Do Something", "target": "My Document", "type": "Activity Output"},

 {"source": "My Document", "target": "Operator", "type": "Object Responsible"},

 {"source": "Operator", "target": "Do Something", "type": "Role Activity"}

];

编辑#2:传递给函数的样本数据为data


[{

 itemname: "Hello World", 

 itemtype: "activity", 

 activitiesafter: ["Do Stuff", "Do Other Stuff"]

}]


绝地无双
浏览 175回答 2
2回答

阿波罗的战车

一些自称的最佳实践:当您不需要索引时,尽可能避免使用 for 循环。(参见forEach等)。这样可以减少污染眼睛的变量。尽早“继续”或“返回”以避免嵌套内容(例如return links先出现)尽量减少变量的范围。这也是通过使用 forEach 来实现的function nameTooLong(data, existingLinks, linkSetter) {&nbsp; const moreLinks = data.reduce((links, item) => {&nbsp; &nbsp; if (item.activitiesafter[0] === "DEFAULT") {&nbsp; &nbsp; &nbsp; return links&nbsp; &nbsp; }&nbsp; &nbsp; item.activitiesafter.forEach(activity => {&nbsp; &nbsp; &nbsp; links.push({&nbsp; &nbsp; &nbsp; &nbsp; source: item.itemname,&nbsp; &nbsp; &nbsp; &nbsp; target: activity,&nbsp; &nbsp; &nbsp; &nbsp; type: "activity-activity-after"&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; })&nbsp; &nbsp; return links&nbsp; }, [])&nbsp; return linkSetter(_ => existingLinks.concat(moreLinks))}nameTooLong([{&nbsp;itemname: "Hello World",&nbsp;&nbsp;itemtype: "activity",&nbsp;&nbsp;activitiesafter: ["Do Stuff", "Do Other Stuff"]},{&nbsp;itemname: "shold be ignored",&nbsp;&nbsp;itemtype: "activity",&nbsp;&nbsp;activitiesafter: ["DEFAULT", "nothing there"]}], ['toto'], (fn) => console.log('newlinks: ', fn()))如果您阅读 3.,我们可以做得更好,甚至可以避免使用flatMap操作变量链接function nameTooLong(data, existingLinks, linkSetter) {&nbsp; const moreLinks = data.flatMap(item => {&nbsp; &nbsp; if (item.activitiesafter[0] === "DEFAULT") {&nbsp; &nbsp; &nbsp; return []&nbsp; &nbsp; }&nbsp; &nbsp; return item.activitiesafter.map(activity => ({&nbsp; &nbsp; &nbsp; source: item.itemname,&nbsp; &nbsp; &nbsp; target: activity,&nbsp; &nbsp; &nbsp; type: "activity-activity-after"&nbsp; &nbsp; }))&nbsp; })&nbsp; return linkSetter(_ => existingLinks.concat(moreLinks))}nameTooLong([{&nbsp;itemname: "Hello World",&nbsp;&nbsp;itemtype: "activity",&nbsp;&nbsp;activitiesafter: ["Do Stuff", "Do Other Stuff"]},{&nbsp;itemname: "shold be ignored",&nbsp;&nbsp;itemtype: "activity",&nbsp;&nbsp;activitiesafter: ["DEFAULT", "nothing there"]}], ['toto'], (fn) => console.log('newlinks: ', fn()))

交互式爱情

push 函数仅适用于数组,不适用于对象。而且由于我认为没有任何理由linkLineItems需要成为一个对象,我建议您只需通过初始化它来使其成为一个数组let linkLineItems = [];。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答