Home > database >  How to save one of JSON keys, using the method filter()?
How to save one of JSON keys, using the method filter()?

Time:02-25

I have implemented such a code. I only want to throw one key into the array under the name ("args"). Unfortunately my code returns in line 91 "TypeError: Cannot read properties of undefined (reading 'length')". How to fix it?

All is into async function :)

var objmapPolygon = {}, objmapBsc = {};

    let jsonValueBsc = JSON.parse(fs.readFileSync('eventsBsc.json'));

    let jsonValuePolygon = JSON.parse(fs.readFileSync('eventsPolygon.json'));


    var mapped = jsonValuePolygon.map( 
      function (v) { for (var l in v) {this[l] = v[l];} }, 
      objmapPolygon
    );


     console.log(objmapPolygon)

     var mapps = jsonValueBsc.map( 
      function (v) { for (var l in v) {this[l] = v[l];} }, 
      objmapBsc
    );
    
    console.log(objmapBsc)

    const resultPolygon = mapps.filter(mapp => mapp.length < 7);

    console.log(resultPolygon);

CodePudding user response:

The .map() function returns an array filled with the values returned from the callback function. Your jsonValueBsc.map callback function does not return anything, so it is an array of the default return value undefined. When you access those array elements in mapps.filter(mapp => mapp.length < 7), mapp is undefined, thus you get an error.

Return something from the first mapping so there is a value, preferably an array so .length is meaningful.

  • Related