중첩 배열에서 개체를 이동하고 부모 배열로 이동하는 간단한 방법을 찾으려고합니다. 'moveItem ()'함수를 사용하면 알 수 있듯이 "splice is not a function"이 계속 표시됩니다. 나는 이것을 다시 작성하는 방법을 모르기 때문에 이것이 작동하도록 허용합니다. 도움을 주시면 감사하겠습니다. 요컨대, 항목 [0] .contains []에서 items []로 개체를 이동하려고합니다.
var items = [{
itemIndex: 0,
name: "a box of matches",
examine: "The box is old and damp,you find a single match inside.",
location: 0, // if location === 1000, items is in inventory.
contains: [{
itemIndex: 1,
name: "a match",
examine: "A single match.",
location: 0,
contains: [],
hidden: true,
collectable: true,
useWith: 2,
useWithFail: 0,
useWithFailResponse: "the box is too damp to light the match",
useWithSuccessResponse: null
}], // Contain items inside items using array.
hidden: false, // if hidden, item cannot show up in invetory or room inventory
collectable: true,
useWith: null, // Item that this item can be successfully used with - index or null
useWithFail: 1, // Use with item that fails but has it's own unique fail message - index or null
useWithFailResponse: "the box is too damp to light the match",
useWithSuccessResponse: null
}, {
itemIndex: 2,
name: "a closed cupboard",
examine: "You find a kitchen knife inside",
location: 4,
contains: [],
hidden: false,
collectable: false,
useWith: null,
useWithFail: null,
useWithFailResponse: null,
useWithSuccessResponse: "The match spaks into life!"
}, {
itemIndex: 3,
name: "a kitchen knife",
examine: "It is old and rusty.",
location: 4,
contains: [],
hidden: true,
collectable: true,
useWith: 1,
useWithFail: null,
useWithFailResponse: null,
useWithSuccessResponse: "The match sparks into life!"
}, ];
function moveItem() {
items.push(items[0].contains[0].splice(0, 1));
}
Array.prototype.splice()
보고
삭제 된 요소를 포함하는 배열입니다.
중첩 배열에서 상위 수준까지 특정 개체를 이동하려면 다음 방법을 사용하십시오.
items.push(items[0].contains.splice(0, 1)[0]);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다