简体   繁体   English

如何在 javascript 中循环直到满足条件并将结果添加到数组中?

[英]How to loop in javascript until condition is met and add the results to an array?

I need to push two key/value pairs to an array based on the different conditions.我需要根据不同的条件将两个键/值对推送到一个数组中。

Condition for the first key/value : only paragraph starting with the number.第一个键/值的条件:仅以数字开头的段落。

Condition for the second key/value : all paragraphs after the first key/value pair (until the next paragraph starting with number).第二个键/值的条件:第一个键/值对之后的所有段落(直到下一个以数字开头的段落)。

Here is the html example:这是 html 示例:

<div class="someclass">
    <p><strong>1. Some text</strong></p>
    <p>Some text related to 1.</p>
    <p>Some other <a href="/someurl"><strong> text</strong></a> related to 1.</p>
    <p><strong>2. Some other text</strong></p>
    <p>Some text related to 2.</p>
    <p><strong>3. Can you send me a catalog?</strong></p>
    ...
</div>

I am able to get only one paragraph for the second key/value pair with this javascript code:使用此 javascript 代码,我只能获得第二个键/值对的一段:

var array = [];
var allParagraphs = document.querySelectorAll("someclass p");
for (i = 0; i < allParagraphs.length; i++) {
    if (/^\d/.test(allParagraphs[i].innerText) === true) {
        array.push({
            "key1": allParagraphs[i].innerText,
            "customText": {
                "key2": allParagraphs[i + 1].innerText
            }
        });
    }
}

The output I get is something like:我得到的 output 是这样的:

[{key1: "1. Some text", customText: {key2: "Some text related to 1."}},{key1: "2. Some text", customText: {key2: "Some text related to 2."}},{...}]

And I need to get something like this:我需要得到这样的东西:

[{key1: "1. Some text", customText: {key2: "Some text related to 1. Some other text related to 1."}},{key1: "2. Some text", customText: {key2: "Some text related to 2."}},{...}]

Does this work for you?这对你有用吗?

You can filter out empty key2 if you need如果需要,您可以过滤掉空 key2

 const vals = [...document.querySelectorAll(".someclass p")].reduce((acc, elem) => { const first = elem.firstChild; if (first.tagName === "STRONG" && first.textContent.match(/^\d+\./)) { acc.push({ "key1": elem.textContent, "customtext": { "key2": [""] } }) } else acc[acc.length - 1].customtext.key2[0] += elem.textContent; return acc }, []) console.log(vals)
 <div class="someclass"> <p><strong>1. Some text</strong></p> <p>Some text related to 1.</p> <p>Some other <a href="/someurl"><strong> text</strong></a> related to 1.</p> <p><strong>2. Some other text</strong></p> <p>Some text related to 2.</p> <p><strong>3. Can you send me a catalog?</strong></p> </div>

ES5 ES5

 const elems = document.querySelectorAll(".someclass p"); const acc = []; for (let i=0;i<elems.length;i++) { const first = elems[i].firstChild; if (first.tagName === "STRONG" && first.textContent.match(/^\d+\./)) { acc.push({ "key1": elems[i].textContent, "customtext": { "key2": [""] } }) } else acc[acc.length - 1].customtext.key2[0] += elems[i].textContent; } console.log(acc)
 <div class="someclass"> <p><strong>1. Some text</strong></p> <p>Some text related to 1.</p> <p>Some other <a href="/someurl"><strong> text</strong></a> related to 1.</p> <p><strong>2. Some other text</strong></p> <p>Some text related to 2.</p> <p><strong>3. Can you send me a catalog?</strong></p> </div>

I think the problem is that this:我认为问题在于:

for (i = 0; i < allParagraphs.length; i++) {
    if (/^\d/.test(allParagraphs[i].innerText) === true) {

Should be this:应该是这样的:

for (i = 0; i < allParagraphs.length; i++) {
    while (/^\d/.test(allParagraphs[i].innerText) === true) {

Basically, the IF statement runs once and then proceeds to the next for loop, while the WHILE is running while the condition inthe parentheses is true基本上,IF 语句运行一次,然后进入下一个 for 循环,而 WHILE 正在运行,而括号中的条件为真

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM