【问题标题】:create arrays based on elements of odd and even indexes of another array根据另一个数组的奇偶索引元素创建数组
【发布时间】:2019-08-06 19:03:14
【问题描述】:

我想用sentence 数组的奇偶索引 填充两个数组(sentenceslinks):

这是我尝试但没有成功的方法:

     let link_sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day"; // 
                 
        let sentence =  link_sentence.split(">")
        let sentences= []
        let links = []
        for(let i = 0; i < sentence.length; i += 2) {  
        
            sentences.push(sentence[i]);
        }
        console.log(sentences)

预期的输出应该是:

//let links = ["60-1", "6-2", "16-32"];
//let sentences = ["don't you worry", "I gonna make sure that", "tommorow is  another great day"];

【问题讨论】:

  • sentences 看起来非常接近正确,而您甚至没有尝试计算 links
  • 我不知道应该如何计算链接,是的,如果句子看起来不太接近正确,我永远不会寻求帮助

标签: javascript


【解决方案1】:

您可以匹配部分并在拆分时省略空字符串。

var link_sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day",
    sentences = [],
    links = [];

link_sentence
    .match(/\>[^>]+/g)
    .reduce(
        (r, s, i) => (r[i % 2].push(s.slice(1)), r),
        [links, sentences]
    );

console.log(sentences);
console.log(links);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 让人们头疼 :) 但一如既往地做得很好,尼娜 ^_^
【解决方案2】:

您的初始尝试很接近,如果您稍微修改您的 for 循环,您可以获得您想要的结果。

// remove first value from the array if the value is empty
if (!sentence[0]) {
    sentence.shift();
}

for(let i = 0; i < sentence.length; i++) {  
    if (i % 2 === 0) {
        links.push(sentence[i]);
    } else {
        sentences.push(sentence[i]);
    }
}

【讨论】:

  • 除了句子以&gt;开头,因为第一个链接是空的,然后是一个链接,然后是一个句子,所以你的索引会不正确:)
  • 我认为这很好,因为 OP 似乎理解其背后的逻辑 :) 唯一的缺点可能是链接和句子会“断开连接”
【解决方案3】:

这是一个使用Array.prototype.reduce的简单解决方案:

const sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day".split(">")

const {links, sentences} = sentence.reduce((acc, val, index) => {
  acc[index % 2 === 0 ? 'links' : 'sentences'].push(val);
  return acc;
}, {
  links: [],
  sentences: []
});

console.log(links, sentences);

【讨论】:

    【解决方案4】:

    你可以这样做:

    const link_sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day"
    const sentence =  link_sentence.split(">")
    const sentences = []
    const links = []
    
    sentence.forEach((el, idx) => {
      if (idx % 2 === 0) {
        // if even
        sentences.push(el)
      } else {
        // if odd
        links.push(el)
      }
    })
    
    

    【讨论】:

      【解决方案5】:

      你可以使用:

      let link_sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day"; // 
                       
      let sentence =  link_sentence.split(">")
      let sentences = []
      let links = []
      
      // removing the first element of array: [""]
      sentence.splice(0, 1)
      
      // iterating sentence
      sentence.forEach((item, index) => {
        // if the index is even push to links, else push to sentences
        if (index % 2 === 0) {
          links.push(item)
        } else {
          // trim white spaces
          sentences.push(item.trim())
        }
      })
      console.log(links)
      console.log(sentences)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-19
        • 2021-11-15
        • 2014-09-14
        • 1970-01-01
        • 2021-11-26
        • 2020-05-22
        • 1970-01-01
        相关资源
        最近更新 更多