我正在学习功能性的javascript。还是新手。 我有个问题 : 如何更改此对象
var object = {
"person1": {
"name": "Bob",
"last": "Smith"
},
"person2": {
"name": "John",
"last": "Smith"
},
"person3": {
"name": "Jane",
"last": "Smith"
}
}
进入此。 (最好有一些功能风格)
var object = {
"person1": {
"no": 1,
"name": "Bob",
"last": "Smith"
},
"person2": {
"no": 2,
"name": "John",
"last": "Smith"
},
"person3": {
"no": 3,
"name": "Jane",
"last": "Smith"
}
}
我的第一个想法是使用对象大小并使用长度
制作数组makeArray = function(obj){
for (key in obj) {
var number = [];
if (obj.hasOwnProperty(key)) size++;
number.push(size);
};
return number;
};
所以myArray = makeArray(object);
myArray = [1,2,3]; //1,2,3 instead 0,1,2
所以我得到1,2,3号而不是0,1,2。
但我如何将其推入个人person.properties
感谢,
答案 0 :(得分:0)
要使makeArray
功能起作用,您应该:
代码:
function makeArray(obj){
var number = [], size = 0;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
size++;
number.push(size);
}
}
return number;
}
如果要设置对象的no
属性:
function setNo(obj){
var size = 0;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
size++;
obj[key].no = size;
}
}
}
答案 1 :(得分:0)
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (counter <= 10)
newimg();
else
// here I want to stop the timer so it will not try to create any more `ImageViews` (the array contains only 10).
}
}, 0, 5000);
private void newimg() {
ball[counter] = new ImageView(this);
ball[counter].setTag(counter);
ball[counter].setBackgroundResource(R.mipmap.ball);
int randomx = rand.nextInt(layoutwidth);
int randomy = rand.nextInt(layoutheight);
ball[counter].setX(randomx);
ball[counter].setY(randomy);
rlt.addView(ball[counter]);
counter++;
}