在下面的對象中,我想將屬性名稱thumb
更改爲thumbnail
。我還想將title
的值更改爲包含<span>
標籤。重命名屬性名稱並更改多個對象的值
這是我的目標:
var data = [{
thumb: '/images/01.png',
title: 'My title',
},{
thumb: '/images/02.png',
title: 'My title',
},{
thumb: '/images/03.png',
title: 'My title',
}];
這裏就是我想怎麼看:
var data = [{
thumbnail: '/images/01.png',
title: '<span class="red">title 1</span>',
},{
thumbnail: '/images/02.png',
title: '<span class="red">title 2</span>',
},{
thumbnail: '/images/03.png',
title: '<span class="red">title 3</span>',
}];
這裏就是我試過不工作:
var i=0, count=data.length;
for (i=0;i<=count;i++){
data[i].thumbnail=data[i].thumb;
data[i].title="<span class='red'>"+data[i].title+"<span>";
}
您是否要求一種方法來做到這一點_不手工?如果是這樣,你應該在你的帖子中澄清。 –
@HunterMcMillen詢問我將如何使用JavaScript以編程方式完成此操作。我假設我需要一個for循環,但不能讓它工作。我會在我的文章中澄清。 –