【问题标题】:Javascript array filtering and mappingJavascript数组过滤和映射
【发布时间】:2020-11-23 14:24:33
【问题描述】:

我在反应方面一直问同样的问题,但没有得到明确的答案,所以我决定尝试从概念上推断我想做的事情,看看我是否不能得到一些关于如何进行反应的答案。

filters= ["Bill Johnson", "hasStartDocs"]
mappedArray = 
[
  {
  name:'Larry', 
  docs:{
   startDocs:[{...}]
   },
  workers:{
  sales:["Bill Johnson"]
  }  
]    

所以如果 filter[i] 有一个空格{' '} 检查workers下的所有数组是否有像filter[i]这样的字符串然后映射 基于那个的filteredMappedArray

如果 filter[i] 没有空格,则创建一个新字符串,将字符串的前 3 个字符切片,使该新字符串的第一个字母小写 (thatString = "startDocs") eval(resoStatus.${thatString} .length > 0) 然后像这样映射filteredMappedArray。

因此,对于 [i] 的每个实例,您将拥有一个唯一的地图。所以如果有人点击了 5 个过滤器,每个过滤器都会有一个 filtersMappedArray,我猜如果它们具有相同的 _id,你会使用 .concat() 和 .reduce()。

我不需要有人来帮助处理字符串。我需要在正确的方向上推动如何利用过滤器 [] 和 mappedArray [] 来创建 1 个过滤映射数组。请,谢谢。

for(i=0,i<filters.length,i++){
filters.map(filter => filter.includes(' ') //map mappedArray based on rules above)
filters.map(filter => filter.includes(/^has/ //map mappedArray based on rules above)
}

这给了你

[filteredMappedArray1]
[filteredMappedArray2]

bigArray = filteredMappedArray1.concat(filteredMappedArray2)

smallArray = bigArray.forEach(map //if the map is unique delete it if the map isnt unique keep it but remove all the duplicates)

【问题讨论】:

  • 您需要帮助编写一个函数来获取您提供的这两个数组,并将它们转换为一个或多个不同的数组,对吗?那么输出应该是什么样子呢?
  • 这将是一组 mappedArray 对象
  • "那么如果 filters.length > 0 && 两个过滤条件都满足比较两个过滤数组并且只显示 ===" 的值我很抱歉,你把我弄丢了……? (回答@TKoL 的问题可能会有所帮助,您的输入的更完整示例也会有所帮助——例如,至少一个 匹配任一过滤器的条目,一个仅匹配其中一个的条目等)
  • @MickeyGray - “秀,别说。” :-)
  • 您能否添加预期的输出(代码),以便我们知道您的确切意思?

标签: javascript arrays filter mapping


【解决方案1】:

据我所知,您没有进行任何映射,只是过滤 mappedArray 以将其限制为与 filters 中至少一个过滤器匹配的条目。

如果是这样,如果您没有比mappedArray 中的条目更多的过滤器(如果您这样做,您会采用不同的结构,但这对我来说似乎不太可能) ):

// The `length` check prevents filtering if there are no filters; that's usually how people
// implement filters, but remove it if that's not right in your case
const filteredArray = filters.length === 0 ? mappedArray : mappedArray.filter(entry => {
    // `some` returns `true` if the callback returns a truthy value (and
    // stops loopihng); it returns `false` if it reaches the end of the
    // array without the callback rturning a truthy value. So basically
    // this is saying "return true if any filter matches."
    return filters.some(filter => {
        if (filter.includes(" ")) {
            // Check `workers` arrays
            for (const array of Object.values(entry.workers)) {
                if (array.includes(filter)) {
                    return true;
                }
            }
        } else {
            // Check `docs` for at least one entry for the given type
            const key = /*...strip leading three, change case of fourth...*/;
            const array = entry.docs.key];
            if (array && array.length > 0) { // There's at least one entry
                return true;
            }
        }
        return false;
    });
});

【讨论】:

  • " for (const array of Object.values(entry.workers))" 是我最需要的,谢谢。
  • 我想我可以在状态下运行类似的东西并只返回那些结果,而不是尝试在搜索本身上这样做。
猜你喜欢
  • 1970-01-01
  • 2022-01-02
  • 2022-01-13
  • 2016-03-27
  • 2022-01-14
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
相关资源
最近更新 更多