Josef Henn
Josef Henn

Reputation: 162

Loop through a json, find all keys with a specific first letter and put it into another json

I want to loop through my json and find all keys with their values with the first two letters "QM" Here is my json:

[ { MHF46: 'Ledig',
    MHF181: '06',
    QM6_QMSI2: '1899-12-30  15:36:57',
    Qm_7QMSI5: 'lucas',
    MHENTRY9: '123123',
    MHENTRY3: '2019-08-28'
     } ]  

the Solution should be

QMSI: [{
        QM6_QMSI2: '1899-12-30  15:36:57',
        Qm_7QMSI5: 'lucas'
}]

I have tried this code but it dont work for me:

var results = [];
 var newJson = json[0];
  var len = Object.keys(json[0]).length;
  for (var i = 0; i < len; i++) {
    if (newJson[i].indexOf("QM") == 0) results.push(newJson[i]);
  }
  console.log(results);
}

Upvotes: 0

Views: 1514

Answers (5)

Guillermo G. Smitto
Guillermo G. Smitto

Reputation: 69

Maybe this can help you

var desired_values = {}
json.forEach( function(item) {
  var keys = Object.keys(item);
    keys.filter(j => {if(j.substr(0,1) == 'Q'){
      desired_keys[j] = item[j]
    }})
  })
  console.log(desired_values) 
}
//result -> {QM6_QMSI2: '1899-12-30  15:36:57', Qm_7QMSI5: 'lucas'}

Upvotes: 0

gaetanoM
gaetanoM

Reputation: 42054

You can use Array.map().

For each object in the array build a new object containing only the properties you are looking for:

var json = [{
    "MHF46": 'Ledig',
    "MHF181": '06',
    "QM6_QMSI2": '1899-12-30  15:36:57',
    "Qm_7QMSI5": 'lucas',
    "MHENTRY9": '123123',
    "MHENTRY3": '2019-08-28'
}, {
    "MHF46": 'Ledig',
    "MHF181": '06',
    "QM6_QMSI21": 'xxx1899-12-30  15:36:57',
    "Qm_7QMSI51": 'xxxlucas',
    "MHENTRY9": '123123',
    "MHENTRY3": '2019-08-28'
}];


var result = json.map(function(e) {
    var newe = {};
    for (var i in e) {
        if (i.toUpperCase().startsWith('QM')) {
            newe[i] = e[i];
        }
    };
    return newe;
});

console.log(result);

Upvotes: 1

Shilly
Shilly

Reputation: 8589

Another variation using Object.entries() instead of Object.keys() to get the key/value pairs, so we do not have to reference input[0] twice. And using Object.fromEntries() to turn the result back into an object, instead of mapping or reducing it manually.

const input = [{
  MHF46: 'Ledig',
  MHF181: '06',
  QM6_QMSI2: '1899-12-30  15:36:57',
  Qm_7QMSI5: 'lucas',
  MHENTRY9: '123123',
  MHENTRY3: '2019-08-28'
}];

const output = [
  Object.fromEntries(
    Object.entries( input[0] ).filter(([ prop ]) => prop.toLowerCase().startsWith( 'qm' ))
  )
];

console.log( output );

Upvotes: 1

JasperZelf
JasperZelf

Reputation: 2844

There are many ways you could do this, here is an example with filter and reduce

const input = [ 
{ MHF46: 'Ledig',
    MHF181: '06',
    QM6_QMSI2: '1899-12-30  15:36:57',
    Qm_7QMSI5: 'lucas',
    MHENTRY9: '123123',
    MHENTRY3: '2019-08-28'
     } 
];

const output = Object.keys(input[0])
  .filter(key => key.toLowerCase().substring(0, 2)==='qm')
  .reduce((obj, key) => {
    obj[key] = input[0][key];
    return obj;
  }, {});
  
console.log(output);

Upvotes: 1

Bahador Raghibizadeh
Bahador Raghibizadeh

Reputation: 1265

var json = [ { MHF46: 'Ledig',
    MHF181: '06',
    QM6_QMSI2: '1899-12-30  15:36:57',
    Qm_7QMSI5: 'lucas',
    MHENTRY9: '123123',
    MHENTRY3: '2019-08-28'
     } ]  
var results = JSON.parse(JSON.stringify(json[0]));
Object.keys(results).forEach((key)=>{
	if(!key.toLocaleLowerCase().startsWith("qm"))
		delete results[key]
})
console.log(results);

Upvotes: 5

Related Questions