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

时间:2019-09-19 09:16:51

标签: javascript node.js

我试图通过获取每个以checkpoint开头的键并输出其值来过滤对象。目前,我只能输出键,而不是值。下面,我有一个简单的对象。我正在使用过滤器和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 个答案:

答案 0 :(得分:6)

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)

答案 1 :(得分: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);

答案 2 :(得分:2)

另一种替代方法是使用测试,如果需要区分大小写,请使用i标志

/^checkpoint/i
  • ^-字符串的开头
  • 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[0-6]?/`
  • [0-6]?-这将允许使用0到6之间的任意数字(可选)

答案 3 :(得分:0)

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

请尝试上面的代码

答案 4 :(得分:0)

另一种无需使用.map .keys.entries这样的衬里整理(虽然很棒)的方法。在for-in的键上有一个object循环。

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);

答案 5 :(得分:0)

android {

buildTypes {
        applicationVariants.all { variant ->
            variant.outputs.all {
                outputFileName = "Your_Desired_Name.apk"
            }
        }
}