![](/img/trans.png)
[英]How can I add elements from an array to an empty object in JavaScript?
[英]How can I add an object into an array using javascript?
我在 app.js 文件中有一個數組,如下所示:
var data = [
{id:1, firstName: 'John', lastName: 'Smith'},
{id:2, firstName: 'Jane', lastName: 'Smith'},
{id:3, firstName: 'John', lastName: 'Doe'}
];
現在,我正在使用 Node.js 並嘗試在 module.js 文件中創建一個函數調用 addEmployee()。 addEmplyee() 只需要兩個參數,被添加的 firstName 和 lastName。 應該使用 underscore.js 將 id 值計算為比當前最大 id 多 1 的值。 這是我在 module.js 文件中的代碼,但它不起作用。
var _ = require('underscore');
module.exports = {
addEmployee: function (firstName, lastName) {
function Person(id, firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = function (data) {
var id = _.max(data, function (data) {
return data.id;
});
return id.id + 1;
}
var person = new Person(id, firstName, lastName);
data.push(person);
return data;
}
}
}
有人可以幫忙嗎? 非常感謝您!
嘗試這個:
var _ = require('underscore');
module.exports = {
addEmployee: function (firstName, lastName) {
function Person(id, firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = (function (data) {
var id = _.max(data, function (data) {
return data.id;
});
return id.id + 1;
})(); //IIFE to invoke function immidiately and thus assign value to id
data.push({id: this.id,firstName: this.firstName,lastName: this.lastName});
return data;
}
}
}
應該是這樣的:
var _ = require('underscore');
// import data
module.exports = {
addEmployee: function (firstName, lastName) {
// remove id
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = (function (data) {
var id = _.max(data, function (data) {
return data.id;
});
return id.id + 1;
})();
}
// put OUTSIDE - dont need id here
var person = new Person(firstName, lastName);
data.push(person);
return data;
}
}
首先,您無論如何都不會調用這個內部函數/構造函數,因此它不會添加到數組中。 其次不要在函數內定義Person
(它會在每次調用時被定義為新事物 - 每個新Person
都是新類型)。 第三件事是,如果你調用你的代碼,它會導致無限循環(在構造函數中對 self 調用 new 將在下一個新實例中做同樣的事情,依此類推)。 另外你在哪里定義/導入數據?
var _ = require('underscore');
// probably it'd be even better to move this to separate module
function Person(id, firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
};
module.exports = {
addEmployee: function (firstName, lastName) {
data.push(new Person((_.max(data, function (data) {
return data.id;
}) + 1), firstName, lastName));
}
}
我也建議lodash
而不是undescore
(簡易替換,但更好的書面)。
如果您只需要保留數據{ id, firstName, lastName }
,則不需要新類型Person
:
module.exports = {
addEmployee: function (firstName, lastName) {
var id = (_.max(data, function (data) {
return data.id;
}) + 1);
data.push({ id: id, firstName: firstName, lastName: lastName });
}
}
如果您使用節點 6.X 或 4.X+(不確定 es6 表示法是否適用於所有 4.X,當然 4.4+ 會這樣做)和'use strict';
它可以更短:
'use strict';
var _ = require('underscore');
module.exports = {
addEmployee: function (firstName, lastName) {
var id = (_.max(data, function (data) {
return data.id;
}) + 1);
data.push({ id, firstName, lastName });
}
}
最后,我建議不要改變數據(副作用大多是壞的)-我會將其轉換為類似的東西(假設節點可以執行 es6):
'use strict';
const _ = require('underscore');
module.exports = {
// note that data comes as input
addEmployee: function (data, firstName, lastName) {
// obtain next id
const id = (_.max(data, function (data) {
return data.id;
}) + 1);
// return new data (old data + new object, but without mutation of initial object)
return [...data, { id, firstName, lastName }];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.