user10021926
user10021926

Reputation:

How to filter and get each object key's value using startsWith?

I am trying to filter an object by getting each key starting with checkpoint and outputting its value. At the moment, I am only able to output the key rather than the value. Below, I have a simple object. I am using the filter and startsWith. How can I get the values instead?

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' ]

Upvotes: 8

Views: 4047

Answers (6)

Nick Parsons
Nick Parsons

Reputation: 50834

One way you could do this is to use Object.entries() instead of Object.keys() to get an array of key-value pairs. Then use .filter() to keep all entries which have a key that start with "checkpoint" and .map() to map your inner arrays to their value:

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

Upvotes: 3

Syed Afzal
Syed Afzal

Reputation: 150

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)

Upvotes: 0

ambianBeing
ambianBeing

Reputation: 3529

Another way to do it without using one liner tidy-ups (awesome though) like .map .keys or .entries. With a single for-in loop on keys of the 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);

Upvotes: 0

Code Maniac
Code Maniac

Reputation: 37755

One more alternative is to use test, and i flag if you need case insensitivity

/^checkpoint/i
  • ^ - Start of string
  • checkpoint - match word 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)

If you need to match a range of number after word checkpoint, you can extend the pattern

 `/^checkpoint[0-6]?/`
  • [0-6]? - This will allow any number between 0 to 6, ( optional )

Upvotes: 3

Sudhakar
Sudhakar

Reputation: 585

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

Please try the above code

Upvotes: 1

zmag
zmag

Reputation: 8251

Use Array.prototype.map with data variable.

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)

Upvotes: 8

Related Questions