简体   繁体   English

我不知道如何解决这个问题,你能帮我吗?

[英]I have no idea how to solve this problem, can you help me?

I have no idea how to solve this problem, can you help me?我不知道如何解决这个问题,你能帮我吗?

How to transform this:如何转换这个:

const array = [["1", ["2", ["3"]]]];

In this (and consider that the array can be an infinite loop):在此(并考虑数组可以是无限循环):

<p>
    1
    <p>
        2<p>3</p>
    </p>
</p>;

Not this:不是这个:

array.forEach(el => {
    el.forEach(el => {
        el.forEach(el =>{

        })
    })
})

You need to use recursion.您需要使用递归。 Other answers seem to make this unnecessarily complicated.其他答案似乎使这变得不必要地复杂。 All you need is this:你只需要这样:

function makeP(arr) {
    if (arr.length == 1) {
        return "<p>" + arr[0] + "</p>";
    }
    return "<p>" + arr[0] + makeP(arr[1]) + "</p>";
}

// Remove the unneeded extra nested array
const array = ["1", ["2", ["3"]]];

console.log(makeP(array));
// output: "<p>1<p>2<p>3</p></p></p>"

If this solved your problem, then you can mark it as correct.如果这解决了您的问题,那么您可以将其标记为正确。

NOTE: You can't actually nest <p> elements, so this is with <divs>注意:你实际上不能嵌套<p>元素,所以这是<divs>

(This works even if you have a weird array like ["1", "2", ["3", [["5", "6"]]]] ) (即使你有一个奇怪的数组,如["1", "2", ["3", [["5", "6"]]]]也可以使用)
Use a recursive function:使用递归 function:

 const array = [["1", ["2", ["3"]]]]; document.body.innerHTML += createDivs(array) function createDivs (array) { let result = "<div>" array.forEach(elem=>{ if (typeof elem === "string") { result += elem } else if (typeof elem === "object") { // Arrays are objects, too result += createDivs(elem) } }) result += "</div>" return result }
 div { border: 1px solid black; padding: 10px; }

You could check the value and if an array map the values with p tag otherwise take only the value.您可以检查该值,如果数组 map 带有p标签的值,否则只取该值。

 const format = value => Array.isArray(value)?`<p>${value.map(format).join('')}</p>`: value, data = ["1", ["2", ["3"]]], result = format(data); console.log(result);

Thank you all: This allowed me to understand how to achieve it is example below:谢谢大家:这让我明白了如何实现它是下面的例子:

 const demo = [ { content: "ligne 1", items: [], }, { content: "ligne 2", items: [ { content: "ligne 2.1", items: [ { content: "ligne 2.1.1", items: [], }, { content: "ligne 2.1.2", items: [], }, ], }, ], }, { content: "ligne 3", items: [], }, ]; const type = "ol" const createList = (items, type) => { let li = ""; const loop = (items, type) => { items.forEach((el) => { if (el.items.length == 0) { li += `<li>${el.content}</li>`; return; } li += `<li>${el.content}</li><${type}>`; loop(el.items, type); li += `</${type}>`; }); }; loop(items, type); return li; }; const result = createList(demo, type) console.log(`<${type}>`,result,`</${type}>`) document.getElementById('container').innerHTML = result
 <ol id='container'></ol>

This is not practical.这是不切实际的。

 const array = [["1", ["2", ["3"]]]]; const result = JSON.stringify(array).replace(/"|,|^.|.$/g, "").replace(/\[/g, "<p>").replace(/]/g, "</p>") console["log"](result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript我怎么解决这个问题,请帮帮我 - How can i solve this problem in javascript, please help me 我的图像没有出现在画布上有问题,你们可以帮我吗? - i have a problem with my image not appearing on my canvas can you guys help me? 请帮我解决这个问题我试图解决范围提取 - Please Help me solve this problem i have trying to solve Range extraction 我如何 map 反应 js 中的一个数组我试过了但是有一些问题谁能帮我 - how do i map an array in react js i have tried but there is some problem can anyone help me 你能帮我解决这个关于 json 的错误吗? - Can you help me to solve this error about json? 您能帮我决定这个JavaScript设计问题吗? - Can you help me decide on this JavaScript design problem? JS中的下拉列表,想要使用PHP保存在数据库中。 我不知道,能帮我吗? - Drop-down list in JS and want to save in database using PHP. I have no idea, can help me? React.js 我在 prod 中有 Bug,但在 dev 模式下没有,你能帮帮我吗 - React.js i have Bug in prod but not in dev mode, can you help me 我有 html 代码,里面有 JS,我有一个问题,提交按钮没有给我警报 window,你能纠正我的代码吗? - i have html code with JS in it, i have a problem that the submit button not giving me an alert window, can you correct my code? 你能帮我这个功能吗 - can you help me with this function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM