看着这个太久了,我已经把我的大脑炸了。使用以下小 JavaScript:
var words = 'one two three four one two three';
wordArray = words.split(' ');
var newArray = [];
var words = {};
words.word;
words.count;
$.each(wordArray, function (ix, val) {
if ($.inArray(wordArray[ix], newArray) > -1) {
newArray[wordArray[ix]].count++;
}
else {
console.log('that wasnt in the array');
newArray.push(wordArray[ix]);
newArray[wordArray[ix]].count = 1;
}
});
我得到错误cannot set property 'count' of undefined
。为什么我不能动态添加属性并让任何工作正常?