简体   繁体   English

使用函数将对象添加到对象数组中?

[英]adding an object into an array of objects with a function?

I understand the commands needed to add an object to an array.我了解将对象添加到数组所需的命令。 I know its array.push(object) .我知道它的array.push(object)

However, say I have this array of objects:但是,假设我有这个对象数组:

 const artists = [{ "rank": 1, "name": "Rakim", "year": 1985, "album": "Paid in Full", }, { "rank": 2, "name": "Nas", "year": 1994, "album": "Illmatic" } ]

If i needed to add a new one with a function.如果我需要添加一个具有功能的新功能。 How would I do so?我该怎么做? And i want to make it a function that would allow me to add more than one in the future.我想让它成为一个功能,允许我在未来添加多个。

So something like,所以像,

function addArtist(array {}) {
  return array;
}

console.log(addArtist(array {
  new object here
}));

You need to pass an array of objects to the function and it to the initial one artists (which should be let ) using the spread operator :您需要使用spread operator将一array对象传递给函数,并将其传递给最初的artists (应该是let ):

 let artists = [ { "rank": 1, "name": "Rakim", "year": 1985, "album": "Paid in Full" }, { "rank": 2, "name": "Nas", "year": 1994, "album": "Illmatic" } ] function addArtist (artistsToAdd) { artists.push(...artistsToAdd); return artists; } const objects = [ { "rank": 3, "name": "Bob", "year": 1995, "album": "Album3" }, { "rank": 4, "name": "Nas", "year": 1992, "album": "Album4" } ]; console.log(addArtist(objects));

You can also use concat to add the objects in the list.您还可以使用concat将对象添加到列表中。

In case you want to separate your code into multiple files and you don't want to pollute your global scope with global artists variable or you have multiple variables with artists interface, you can use:如果您想将代码分成多个文件,并且不想使用全局artists变量污染全局范围,或者您有多个artists界面变量,则可以使用:

function addToArtists(artists, ...newArtists) {
  artists.push(...newArtists);
  return artists;
}

The script above modifies the array passed as the first parameter because arrays are reference types in JavaScript.上面的脚本修改了作为第一个参数传递的数组,因为数组是 JavaScript 中的引用类型。

However, the function proposed above (without type checks) is pretty much general addToArray function.然而,上面提出的函数(没有类型检查)是非常通用的addToArray函数。 Therefore, from the programming style perspective I would consider to go with a more precise implementation such as:因此,从编程风格的角度来看,我会考虑采用更精确的实现,例如:

function addArtist(artists, rank, name, year, album) {
  artists.push({rank, name, year, album});
  return artists;
}

If you only need to add a new artist one at a time - eg, from input by the user on the page, you could do:如果您一次只需要添加一个新艺术家 - 例如,根据用户在页面上的输入,您可以执行以下操作:

 const artists = [{ "rank": 1, "name": "Rakim", "year": 1985, "album": "Paid in Full" }, { "rank": 2, "name": "Nas", "year": 1994, "album": "Illmatic" } ] function addNewArtist(r, n, y, a) { artists.push({"rank": r, "name": n, "year": y, "album": a}); } addNewArtist(3, "Bob", 1999, "His new one"); addNewArtist(4, "Joe", 2015, "His latest one"); console.log(artists);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM