我在這里有一段代碼,我想識別最后一個元素paragraph 3并添加一些類似 - last item的文本,輸出將是paragraph 3 - last item.
我更喜歡它的遞回,因為物件中的子項數量沒有限制。
obj = {
content: [
{ text: "paragraph 1" },
{
content: [
{ text: "paragraph 2" },
]
},
{ text: "paragraph 3" },
]
}
另一個例子是這個,它的輸出應該是 paragraph 5 - last item
obj = {
content: [
{ text: "paragraph 1" },
{
content: [
{ text: "paragraph 2" }
]
},
{ text: "paragraph 3" },
{
content: [
{ text: "paragraph 4" },
{
content: [
{ text: "paragraph 5" }
]
}
]
}
]
}
uj5u.com熱心網友回復:
一個簡單的實作。檢查鍵名,如果是內容,它會用最后一個元素呼叫自己。否則,它會回傳它。
const obj1 = {
content: [{
text: "paragraph 1"
}, {
content: [{
text: "paragraph 2"
}]
}, {
text: "paragraph 3"
}]
};
const obj2 = {
content: [{
text: "paragraph 1"
},
{
content: [{
text: "paragraph 2"
}]
}, {
text: "paragraph 3"
},
{
content: [{
text: "paragraph 4"
},
{
content: [{
text: "paragraph 5"
}]
}
]
}
]
}
function getLatestParagraph(obj) {
for (const key in obj) {
if (key === "content") return getLatestParagraph(obj[key].pop());
return obj[key];
}
}
console.log(getLatestParagraph(obj1))
console.log(getLatestParagraph(obj2))
uj5u.com熱心網友回復:
一種方法是回圈并獲取對text一個變數的參考,并在每個回圈中更新它。在所有回圈結束后,變數將指向最后一個,text因此您只需添加' - last item'它即可。
這是一個作業示例:
const obj1 = {
content: [{
text: 'paragraph 1'
},
{
content: [{
text: 'paragraph 2'
}]
},
{
text: 'paragraph 3'
},
{
content: [{
text: 'paragraph 4'
},
{
content: [{
text: 'paragraph 5'
}]
},
],
},
],
}
const obj2 = {
content: [{
text: 'paragraph 1'
},
{
content: [{
text: 'paragraph 2'
}]
},
{
text: 'paragraph 3'
},
],
}
let last
function getLast(o) {
for (let key in o) {
if (o[key] instanceof Array) {
for (let value of o[key]) {
getLast(value)
}
} else if (key == 'text') {
last = o
}
}
}
getLast(obj1)
last.text = ' - last item'
getLast(obj2)
last.text = ' - last item'
console.log('\nobj1 = ', JSON.stringify(obj1, null, 1))
console.log('\nobj2 = ', JSON.stringify(obj2, null, 1))
編輯:如果您真正想要的只是獲取最后一項的文本,那么您也可以這樣做:
const obj1 = {
content: [{
text: 'paragraph 1'
},
{
content: [{
text: 'paragraph 2'
}]
},
{
text: 'paragraph 3'
},
{
content: [{
text: 'paragraph 4'
},
{
content: [{
text: 'paragraph 5'
}]
},
],
},
],
}
const obj2 = {
content: [{
text: 'paragraph 1'
},
{
content: [{
text: 'paragraph 2'
}]
},
{
text: 'paragraph 3'
},
],
}
let last
function getLast(o) {
for (let key in o) {
if (o[key] instanceof Array) {
for (let value of o[key]) {
getLast(value)
}
} else if (key == 'text') {
last = o
}
}
return last.text
}
const last1 = getLast(obj1)
console.log(last1)
const last2 = getLast(obj2)
console.log(last2)
uj5u.com熱心網友回復:
您可以映射content屬性并回傳每個元素。如果該content屬性存在,則相同的條件將再次運行,否則將列印該text屬性。
let lastItem = {};
function recursive(node) {
if (node.content) {
node.content.map(element => recursive(element));
} else if (node.text) {
lastItem = node.text;
}
}
obj.content.map(node => recursive(node));
console.log(lastItem " - last item");
作業演示:https : //jsfiddle.net/4f7Lt3ue/
uj5u.com熱心網友回復:
這是一個遞回解決方案,它采用內容中的最后一項,只要有一項,或者只回傳當前物件的文本:
const obj1 = {"content":[{"text":"paragraph 1"},{"content":[{"text":"paragraph 2"}]},{"text":"paragraph 3"}]};
const obj2 = {"content":[{"text":"paragraph 1"},{"content":[{"text":"paragraph 2"}]},{"text":"paragraph 3"},{"content":[{"text":"paragraph 4"},{"content":[{"text":"paragraph 5"}]}]}]}
const getLatestParagraph = obj =>
!obj.content?.at(-1) // if there isn't a last item in content
? obj.text // take the current ones text
: getLatestParagraph(obj.content.at(-1)) // or run the function on the new last item
console.log(getLatestParagraph(obj1))
console.log(getLatestParagraph(obj2))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/359907.html
標籤:javascript 数组 目的 递归 javascript对象
