通过"添加"来增加Javascript对象值数组

时间:2015-11-15 11:55:54

标签: javascript jquery

我有这个javascript对象:

{"Vanilla": 36, "Chocolate": 50, "Stracciatella": 24, "Coffee": 18}

我希望"添加"这个数组:

["Vanilla", "Amarena", "Chocolate", "Pistachio"]

我想要的结果是:

{
    "Vanilla": 37,
    "Chocolate": 51,
    "Stracciatella": 24,
    "Coffee": 18,
    "Amarena": 1,
    "Pistachio": 1
}

这就是我的尝试:

var me = ["Vanilla", "Amarena", "Chocolate", "Pistachio"]
var all = {
    "Vanilla": 36,
    "Chocolate": 50,
    "Stracciatella": 24,
    "Coffee": 18
}
var keys = Object.keys(all);
for (var i = 0; i < me.length; i++) {
    if (me[i] in keys) {
        console.log("keys:" + keys + " - " + me[i] + " in keys");
    } else {
        console.log("keys:" + keys + " - " + me[i] + " not in keys");

    };
}

console.log(keys);

4 个答案:

答案 0 :(得分:3)

以下代码允许您根据数组stock中的键增加对象stockUpdate中的值:

var stockUpdate = ["Vanilla", "Amarena", "Chocolate", "Pistachio"];
var stock = {
    "Vanilla": 36,
    "Chocolate": 50,
    "Stracciatella": 24,
    "Coffee": 18
};

for (var i=0; i < stockUpdate.length; i++) {
    var stockType = stockUpdate[i];
    if (stock[stockType] === undefined) {
        stock[stockType] = 0;
    }
    stock[stockType]++;
}

答案 1 :(得分:1)

这有效

你的小提琴

中代码的第一部分
var me = ["Vanilla", "Amarena", "Chocolate", "Pistachio"]
var all = {
    "Vanilla": 36,
    "Chocolate": 50,
    "Stracciatella": 24,
    "Coffee": 18
}

现在回答:

me.forEach(function(key) { // forEach iterates through an array 
    all[key] = (all[key] || 0) + 1; // create new item if it doesn't exist
});
  

forEach()方法每个数组元素执行一次提供的函数。

有关.forEach的更详细说明,请参阅MDN documentation,其中上述说明来自

由于此问题已标记为jQuery,因此我们可以添加一些jQuery代码,仅用于笑声

$.each(me, function(unusedIndex, key) {
    all[key] = (all[key] || 0) + 1;exist
});
  

jQuery.each()通用迭代器函数,可用于无缝迭代对象和数组。具有length属性的数组和类似数组的对象(例如函数的参数对象)由数字索引迭代,从0到length-1。其他对象通过其命名属性进行迭代。 - 来源http://api.jquery.com/jquery.each/

我在这里与自己玩耍 - http://jsfiddle.net/7ephp3f4/1/

答案 2 :(得分:1)

你快到了,

var me = ["Vanilla", "Amarena", "Chocolate", "Pistachio"]
var all = {
    "Vanilla": 36,
    "Chocolate": 50,
    "Stracciatella": 24,
    "Coffee": 18
}
var keys = Object.keys(all);
for (var i = 0; i < me.length; i++) {
    if (keys.indexOf(me[i]) >= 0) {
        console.log("keys:" + keys + " - " + me[i] + " in keys \n");
        all[me[i]]++;
    } else {
        console.log("keys:" + keys + " - " + me[i] + " not in keys \n");
        all[me[i]] = 1;
    };
}

console.log(all);

你也可以使用reduce to&#34; merge&#34;将newItems转换为items

var items = {"Vanilla": 36, "Chocolate": 50, "Stracciatella": 24, "Coffee": 18}
var newItems = ["Vanilla", "Amarena", "Chocolate", "Pistachio"];

var result = newItems.reduce(function(pre, curr) {
        // use double == to check when the item is either undefined or null
        pre[curr] = (pre[curr] == null? 0 : pre[curr]) + 1;
        return pre;
// pass items as the initial data to reduce
}, items);

console.log(result);
  

reduce()方法对累加器和数组的每个值(从左到右)应用一个函数,将其减少为单个值。

在这种情况下,我从左到右缩小newItems列表,计算items对象中的计数,并返回计算的项目。

答案 3 :(得分:0)

这是使用http://underscorejs.org的好时机:

var me = ["Vanilla", "Amarena", "Chocolate", "Pistachio"];

var all = {
  "Vanilla": 36,
  "Chocolate": 50,
  "Stracciatella": 24,
  "Coffee": 18
};

_.each(me, function(name) {
  if (_.has(all, name)) {
    all[name]++;
  } else {
    all[name] = 1;
  }
});
相关问题