简体   繁体   English

如何不映射缺少子值的对象

[英]How to not map object that has a child value missing

I am mapping some data in array. 我在数组中映射一些数据。 The array length is 3 so I get 3 versions. 数组长度是3,所以我得到3个版本。 If a child value is '' or undefined I don't want it to map that object at all just the ones that have all values. 如果子值是''undefined我不希望它仅将具有所有值的对象映射到该对象。

const data = {
  status4Weeks: "2",
  status12Weeks: ""
}; 

status12weeks should be skipped as its empty. status12weeks应该被跳过为空。 I create a new array for my mapping adding in some other values. 我为映射创建了一个新数组,并添加了其他一些值。

const newdata = [
      {
        title: "4 Weeks",
        statusWeek: data.status4Weeks,
        color: status4WeeksColor,
        numWeeks: 4
      },
      {
        title: "12 Weeks",
        statusWeek: data.status12Weeks,
        color: status12WeeksColor,
        numWeeks: 12
      }
    ];

4 weeks should not be mapped as the essential value that drives this is missing. 不应将4周映射为导致缺失的基本值。

{newdata.map(v => (
            <ListItem dense={this.props.dense} alignItems="flex-start">
              <ListItemText
                primary={
                  <React.Fragment>
                    Predicted score - <strong>{v.title}</strong>
                  </React.Fragment>
                }
                secondary={
                  <React.Fragment>
                    <Typography component="span" color="textPrimary">
                      Predicted date{" "}
                      <i
                        style={{ "font-size": "1em" }}
                        className="zmdi zmdi-calendar mx-2"
                      />{" "}
                      {moment(data.vulnerabilityDate)
                        .add(v.numWeeks, "weeks")
                        .format("MMM DD, YYYY")}
                    </Typography>
                  </React.Fragment>
                }
              />
              <ListItemSecondaryAction>
                <span
                  style={{
                    "font-size": "1rem",
                    "background-color": v.color,
                    "min-width": "45px"
                  }}
                  className="vulnerability badge badge-primary"
                >
                  {v.statusWeek}
                </span>
              </ListItemSecondaryAction>
            </ListItem>
          ))}

Here is a codesandbox - https://codesandbox.io/s/wq72p92295 这是一个codeandbox- https ://codesandbox.io/s/wq72p92295

I've tried using a ternary operator, inside my mapping : 我尝试在映射中使用三元运算符:

{v.status.Week !== '' || undefined ? ...

Mapping an array will still return remove an index from an array. 映射数组仍将返回从数组中删除索引。

Using filter would work in this case. 在这种情况下,可以使用过滤器

You should use something like: 您应该使用类似:

array.filter(e=> (e!==undefined && e.length>0)).map(e=> /*your map stuff*/)

Hope this helps, 希望这可以帮助,

You can simply use reduce . 您可以简单地使用reduce

inside reduce you can check for existence of value if it is true than only add to final output. 在reduce中,您可以检查值是否存在,而不是仅添加到最终输出中。

newdata.reduce((op,inp) => {
  if(inp) {
    op.push(inp)
  }
  return inp
},[])

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

相关问题 如何知道地图中的对象是否具有值 - How to know if a object inside a map has a value 如何找到具有匹配值的子 object 的文档? - How to find documents with child object that has matching value? 为什么缺少的 function 参数的值为“[object Object]”? - Why a missing function parameter has a value of “[object Object]”? 如何在lodash地图对象中找到最后一个孩子? - How to find the last child in lodash map object? 如何确定JSON对象是否具有子项 - How to determine if JSON object has child items 如何确定对象是否具有某些元素的子元素? - how determine if object has child of certain element? 如何在 object 的深层嵌套数组和 javascript 的数组中获取具有特定值的子项的父项? - How can get parent of a child that has a specific value in a deep nested array of object and array with javascript? 如何检查字符串到对象数组的映射在对象中是否具有给定值? - How do I check if a Map of Strings to Arrays of Objects has a given value within the Object? 如何检查 Javascript Map 是否有对象键 - How to check if Javascript Map has an object key 对于定义为枚举值到字符串的映射的类型,如何使用流检查映射对象文字是否具有每个枚举值的键? - For a type defined as a map of enum values to strings, how to check with flow that the map object literal has a key for every enum value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM