如何使用startsWith过滤并获取每个对象键的值?

6
我正在尝试通过获取以checkpoint开头的每个键并输出其值来过滤对象。目前,我只能输出键而不是值。下面是一个简单的对象。我正在使用filter和startsWith。如何获取值?

var data = {
  practicals: '0',
  checkpoint01: '0',
  checkpoint02: '0',
  checkpoint03: '0',
  checkpoint04: '0',
  checkpoint05: '0',
  checkpoint06: '0',
  checkpoint07: '0',
  checkpoint08: '0',
  checkpoint09: '0',
  checkpoint10: '0',
  total: '0'
}

var res = Object.keys(data).filter(v => v.startsWith('checkpoint'))

console.log(res)

// This is the current output: ["checkpoint01", "checkpoint02", "checkpoint03", "checkpoint04", "checkpoint05", "checkpoint06", "checkpoint07", "checkpoint08", "checkpoint09", "checkpoint10"]

// This is the expected output I want: [ '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' ]

6个回答

7
使用Array.prototype.mapdata变量。

var data = {
  practicals: '0',
  checkpoint01: '0',
  checkpoint02: '0',
  checkpoint03: '0',
  checkpoint04: '0',
  checkpoint05: '0',
  checkpoint06: '0',
  checkpoint07: '0',
  checkpoint08: '0',
  checkpoint09: '0',
  checkpoint10: '0',
  total: '0'
}

var res = Object.keys(data).filter(v => v.startsWith('checkpoint')).map(e => data[e])

console.log(res)


3

你可以使用Object.entries()代替Object.keys()来获取一个键值对数组。然后使用.filter()保留所有以"checkpoint"开头的键值对,使用.map()将内部数组映射到它们的值:

const data = { practicals: '0', checkpoint01: '0', checkpoint02: '0', checkpoint03: '0', checkpoint04: '0', checkpoint05: '0', checkpoint06: '0', checkpoint07: '0', checkpoint08: '0', checkpoint09: '0', checkpoint10: '0', total: '0' };

const res = Object.entries(data)
              .filter(([k]) => k.startsWith('checkpoint'))
              .map(([,v]) => v);

console.log(res);


还有一个问题,我该如何获取范围呢?例如,我想获取从checkpoint1到checkpoint6的范围。 - user10021926
1
@User123 最简单的方法可能是使用正则表达式,可以参考Code Maniac的答案 - Nick Parsons

3

另一种选择是使用测试,如果需要不区分大小写,则使用 i 标志。

/^checkpoint/i
  • ^ - 字符串起始位置
  • checkpoint - 匹配单词"checkpoint"

var data = {practicals: '0',checkpoint01: '0',checkpoint02: '0',checkpoint03: '0',checkpoint04: '0',checkpoint05: '0',checkpoint06: '0',checkpoint07: '0',checkpoint08: '0',checkpoint09: '0',checkpoint10: '0',total: '0'}

var final = Object.keys(data)
            .filter(value => /^checkpoint/i.test(value))
            .map(e => data[e])

console.log(final)

如果您需要匹配“checkpoint”后面的一系列数字,您可以扩展该模式。
 `/^checkpoint[0-6]?/`
  • [0-6]? - 这将允许任何介于0到6之间的数字,(可选)

当我将以下代码放入@codemaniac时,范围无法正常工作: .filter(value => /^checkpoint[0-6]?/i.test(value)) .map(e => data[e])``` - user10021926
@User123 [0-6]? 可以匹配一次或不匹配,如果需要匹配更多数字,则需要使用量词。你可以使用 \d*,它表示匹配 0 到 9 之间的任何数字,零次或多次。 - Code Maniac
你能否用代码解释一下?我不太擅长正则表达式和它们的使用。@codemaniac - user10021926
@User123,你可以关注这个数字范围正则表达式生成器,如果你还有任何困难,请告诉我。 - Code Maniac

1
let result = [];
let keysArr = Object.keys(data);
keysArr.forEach((eachKey,index)=>{
    if(eachKey.startsWith('checkpoint')){
        result.push(data[eachKey]);
    }
})

请尝试上述代码。

0

还有一种方法可以做到这一点,而不必使用像.map .keys.entries这样的一行整洁漂亮的代码。只需要在object的键上使用单个for-in循环。

const data = {
  practicals: "0",
  checkpoint01: "0",
  checkpoint02: "0",
  checkpoint03: "0",
  checkpoint04: "0",
  checkpoint05: "0",
  checkpoint06: "0",
  checkpoint07: "0",
  checkpoint08: "0",
  checkpoint09: "0",
  checkpoint10: "0",
  total: "0"
};

let result = [];
for (const key in data) {
  //regex test could be used instead for case-insensitivity
  if (key.startsWith("checkpoint")) {
    result.push(data[key]);
  }
}
console.log(result);


0
var data = {
    practicals: '0',
    checkpoint01: '0',
    checkpoint02: '0',
    checkpoint03: '0',
    checkpoint04: '0',
    checkpoint05: '0',
    checkpoint06: '0',
    checkpoint07: '0',
    checkpoint08: '0',
    checkpoint09: '0',
    checkpoint10: '0',
    total: '0'
};

let res = [];
Object.keys(data).map((key) => {
    console.log(key)
    if(key.startsWith('checkpoint'))
        res.push(data[key])
})
console.log(res)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接