我有1300多个对象的数组。这些物品是牛仔裤,具有腰部,膝部,大腿和袖口等尺寸。
我想为这个数组中的每个对象添加4个新属性:
var getResults = function(){
results = $.map(dbcontent,function(i){
(i).riseProx = "0";
(i).thighProx = "0";
(i).kneeProx = "0";
(i).cuffProx = "0";
当我运行它时,它表示riseProx未定义。我要做的是将这四个新属性添加到每个对象。请帮忙。非常感谢:)。
var getResults = function(){
results = $.map(dbcontent,function(i){
//create counter for proximity
(i).riseProx = 0;
(i).thighProx = 0;
(i).kneeProx = 0;
(i).cuffProx = 0;
//start iterating and adding to counter
//check the rise
if ((i).rise >= (psObject.rise-qInch) && (i).rise <= (psObject.rise+qInch)){
riseProx+=3}
else if ((i).rise >= (psObject.rise-hInch) && (i).rise <= (psObject.rise+hInch)){
riseProx+=2}
else if ((i).rise >= (psObject.rise-inch) && (i).rise <= (psObject.rise+inch)){
riseProx++};
//check the thigh
if ((i).thigh >= (psObject.thigh-qInch) && (i).thigh <= (psObject.thigh+qInch)){
thighProx+=3}
else if ((i).thigh >= (psObject.thigh-hInch) && (i).thigh <= (psObject.thigh+hInch)){
thighProx+=2}
else if ((i).thigh >= (psObject.thigh-inch) && (i).thigh <= (psObject.thigh+inch)){
thighProx++};
//check the knee
if ((i).knee >= (psObject.knee-qInch) && (i).knee <= (psObject.knee+qInch)){
kneeProx+=3}
else if ((i).knee >= (psObject.knee-hInch) && (i).knee <= (psObject.knee+hInch)){
kneeProx+=2}
else if ((i).knee >= (psObject.knee-inch) && (i).knee <= (psObject.knee+inch)){
kneeProx++};
//check the cuff
if ((i).cuff >= (psObject.cuff-qInch) && (i).cuff <= (psObject.cuff+qInch)){
cuffProx+=3}
else if ((i).cuff >= (psObject.cuff-hInch) && (i).cuff <= (psObject.cuff+hInch)){
cuffProx+=2}
else if ((i).cuff >= (psObject.cuff-inch) && (i).cuff <= (psObject.cuff+inch)){
cuffProx++};
//create totalProx to evaluate total proximity or likeness in size
i.totalProx = i.riseProx + i.thighProx + i.kneeProx + i.cuffProx;
console.log(i.totalProx)
});
};
这是整个功能。我要做的是将psObject(选定的牛仔裤大小)与数据库中的每个牛仔裤进行比较,以确定哪条牛仔裤是最接近的匹配。我打算通过创建riseProx等属性并将它们增加1,2和3来实现,具体取决于匹配的接近程度。
答案 0 :(得分:0)
我看不到从(i)
函数返回$.map
项。
另请参阅使用reduce
的映射,如下所示:
var getResults = function(){
return dbcontent.reduce(function(mapped, item){
item.riseProx = "0";
item.thighProx = "0";
item.kneeProx = "0";
item.cuffProx = "0";
mapped.push(item);
return mapped;
}, []);
};