Skip to content

Commit 8a2c514

Browse files
Peter-WFyyx990803
authored andcommitted
Be able to use string type index in array (#5889)
1 parent 080c387 commit 8a2c514

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: src/core/observer/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export function defineReactive (
189189
* already exist.
190190
*/
191191
export function set (target: Array<any> | Object, key: any, val: any): any {
192-
if (Array.isArray(target) && typeof key === 'number') {
192+
if (Array.isArray(target) && (typeof key === 'number' || /^\d+$/.test(key))) {
193193
target.length = Math.max(target.length, key)
194194
target.splice(key, 1, val)
195195
return val

Diff for: test/unit/features/global-api/set-delete.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,23 @@ describe('Global API: set/delete', () => {
9696
expect(vm.$el.innerHTML).toBe('')
9797
}).then(done)
9898
})
99+
100+
it('be able to use string type index in array', done => {
101+
const vm = new Vue({
102+
template: '<div><p v-for="obj in lists">{{obj.name}}</p></div>',
103+
data: {
104+
lists: [
105+
{ name: 'A' },
106+
{ name: 'B' },
107+
{ name: 'C' }
108+
]
109+
}
110+
}).$mount()
111+
expect(vm.$el.innerHTML).toBe('<p>A</p><p>B</p><p>C</p>')
112+
Vue.set(vm.lists, '0', { name: 'D' })
113+
waitForUpdate(() => {
114+
expect(vm.$el.innerHTML).toBe('<p>D</p><p>B</p><p>C</p>')
115+
}).then(done)
116+
})
99117
})
100118
})

0 commit comments

Comments
 (0)