分享
三行代码  ›  专栏  ›  技术社区  ›  Kfyce

如何从JSON获取奇数值?

  •  1
  • Kfyce  · 技术社区  · 5 天前

    我有一个对象数组,我想从JSON中删除一些我不想要的数据。

    [{
      irregular: {
        pulse: [91, 0],
        pulse_quality: [255, 0]
      }
    }, {
      irregular: {
        pulse: [77, 0, 77, 500, 77, 1001, 77, 1504, 77, 2007, 77, 2512, 77, 3015, 77, 3517, 77, 4018, 77, 4521, 77, 5024, 77, 5530, 77, 6033, 77, 6534, 77, 7036, 77, 7539, 77, 8043, 77, 8547, 74, 9049, 74, 13208, 74, 13741, 74, 14243, 74, 14745, 79, 15284, 79, 15827, 79, 16367, 79, 16910, 79, 17450, 79, 17991, 79, 18533, 79, 19075, 79, 19616, 79, 20157, 81, 20659, 81, 21199, 81, 21742, 81, 22282, 81, 22823, 78, 23366, 78, 23909, 78, 24449, 78, 24990, 78, 25532, 80, 26073, 80, 26616, 80, 27156, 80, 27657, 80, 28158, 80, 28699, 80, 29239, 80, 29782, 80, 30322, 80, 30865, 83, 31366, 83, 31905, 83, 32448, 83, 32949, 83, 33488, 83, 34031, 82, 34571, 82, 35115, 82, 35654, 82, 36155, 82, 36698, 82, 37237, 82, 37781, 82, 38320, 82, 38864, 82, 39403, 82, 39947, 82, 40487, 82, 41030, 82, 41570, 82, 42114, 82, 42653, 82, 43197, 82, 43736, 82, 44280, 82, 44819, 82, 45363, 82, 45902, 82, 46446, 82, 46984, 82, 47485, 82, 48029, 82, 48568, 82, 49113, 82, 49651, 82, 50195, 82, 50734, 82, 51279, 82, 51817, 82, 52360, 82, 52862, 82, 53400, 82, 53945, 82, 54483, 82, 54984, 82, 55527, 82, 56066, 82, 56612, 82, 57149, 82, 57694, 82, 58194, 82, 58733, 82, 59277, 82, 59816],
        pulse_quality: [255, 0, 255, 500, 255, 1001, 255, 1504, 255, 2007, 255, 2512, 255, 3015, 255, 3517, 255, 4018, 255, 4521, 255, 5024, 255, 5530, 255, 6033, 255, 6534, 255, 7036, 255, 7539, 255, 8043, 255, 8547, 255, 9049, 255, 13208, 255, 13741, 255, 14243, 255, 14745, 255, 15284, 255, 15827, 255, 16367, 255, 16910, 255, 17450, 255, 17991, 255, 18533, 255, 19075, 255, 19616, 255, 20157, 255, 20659, 255, 21199, 255, 21742, 255, 22282, 255, 22823, 255, 23366, 255, 23909, 255, 24449, 255, 24990, 255, 25532, 255, 26073, 255, 26616, 255, 27156, 255, 27657, 255, 28158, 255, 28699, 255, 29239, 255, 29782, 255, 30322, 255, 30865, 255, 31366, 255, 31905, 255, 32448, 255, 32949, 255, 33488, 255, 34031, 255, 34571, 255, 35115, 255, 35654, 255, 36155, 255, 36698, 255, 37237, 255, 37781, 255, 38320, 255, 38864, 255, 39403, 255, 39947, 255, 40487, 255, 41030, 255, 41570, 255, 42114, 255, 42653, 255, 43197, 255, 43736, 255, 44280, 255, 44819, 255, 45363, 255, 45902, 255, 46446, 255, 46984, 255, 47485, 255, 48029, 255, 48568, 255, 49113, 255, 49651, 255, 50195, 255, 50734, 255, 51279, 255, 51817, 255, 52360, 255, 52862, 255, 53400, 255, 53945, 255, 54483, 255, 54984, 255, 55527, 255, 56066, 255, 56612, 255, 57149, 255, 57694, 255, 58194, 255, 58733, 255, 59277, 255, 59816]
      }
    }, {
      irregular: {
        pulse: [75, 0],
        pulse_quality: [255, 0]
      }
    }, {
      irregular: {
        pulse: [21, 0],
        pulse_quality: [255, 0]
      }
    }, {
      irregular: {
        pulse: [99, 0],
        pulse_quality: [255, 0]
      }
    }]
    

    我只想在脉冲中保存奇数(1、3、5、7、9、11等):

    第二个物体是77,77,77,77,77,77。。。。。。等

    对于脉冲质量,仅保存对号(2、4、6、8、10、12等):

    第一个对象是0。 第二个对象是500100115042007。。。。。。等

    var obj1 = { model: "500", color: "white", constructor: { skill: { json_formatted: "{\"irregular\":{\"pulse_quality\":[255,0],\"pulse\":[91,0]}}" } } };
    var obj2 = { model: "200", color: "black", constructor: { skill: { json_formatted: "{\"irregular\":{\"pulse_quality\":[255,0,255,500,255,1001,255,1504,255,2007,255,2512,255,3015,255,3517,255,4018,255,4521,255,5024,255,5530,255,6033,255,6534,255,7036,255,7539,255,8043,255,8547,255,9049,255,13208,255,13741,255,14243,255,14745,255,15284,255,15827,255,16367,255,16910,255,17450,255,17991,255,18533,255,19075,255,19616,255,20157,255,20659,255,21199,255,21742,255,22282,255,22823,255,23366,255,23909,255,24449,255,24990,255,25532,255,26073,255,26616,255,27156,255,27657,255,28158,255,28699,255,29239,255,29782,255,30322,255,30865,255,31366,255,31905,255,32448,255,32949,255,33488,255,34031,255,34571,255,35115,255,35654,255,36155,255,36698,255,37237,255,37781,255,38320,255,38864,255,39403,255,39947,255,40487,255,41030,255,41570,255,42114,255,42653,255,43197,255,43736,255,44280,255,44819,255,45363,255,45902,255,46446,255,46984,255,47485,255,48029,255,48568,255,49113,255,49651,255,50195,255,50734,255,51279,255,51817,255,52360,255,52862,255,53400,255,53945,255,54483,255,54984,255,55527,255,56066,255,56612,255,57149,255,57694,255,58194,255,58733,255,59277,255,59816],\"pulse\":[77,0,77,500,77,1001,77,1504,77,2007,77,2512,77,3015,77,3517,77,4018,77,4521,77,5024,77,5530,77,6033,77,6534,77,7036,77,7539,77,8043,77,8547,74,9049,74,13208,74,13741,74,14243,74,14745,79,15284,79,15827,79,16367,79,16910,79,17450,79,17991,79,18533,79,19075,79,19616,79,20157,81,20659,81,21199,81,21742,81,22282,81,22823,78,23366,78,23909,78,24449,78,24990,78,25532,80,26073,80,26616,80,27156,80,27657,80,28158,80,28699,80,29239,80,29782,80,30322,80,30865,83,31366,83,31905,83,32448,83,32949,83,33488,83,34031,82,34571,82,35115,82,35654,82,36155,82,36698,82,37237,82,37781,82,38320,82,38864,82,39403,82,39947,82,40487,82,41030,82,41570,82,42114,82,42653,82,43197,82,43736,82,44280,82,44819,82,45363,82,45902,82,46446,82,46984,82,47485,82,48029,82,48568,82,49113,82,49651,82,50195,82,50734,82,51279,82,51817,82,52360,82,52862,82,53400,82,53945,82,54483,82,54984,82,55527,82,56066,82,56612,82,57149,82,57694,82,58194,82,58733,82,59277,82,59816]}}" } } };
    var obj3 = { model: "600", color: "orange", constructor: { skill: { json_formatted: "{\"irregular\":{\"pulse_quality\":[255,0],\"pulse\":[75,0]}}" } } };
    var obj4 = { model: "100", color: "red", constructor: { skill: { json_formatted: "{\"irregular\":{\"pulse_quality\":[255,0],\"pulse\":[21,0]}}" } } };
    var obj5 = { model: "900", color: "blue", constructor: { skill: { json_formatted: "{\"irregular\":{\"pulse_quality\":[255,0],\"pulse\":[99,0]}}" } } };
    
    var multiobject = {obj1, obj2, obj3, obj4, obj5};
    
    var edge = [];
    
    Object.keys(multiobject).map(function(objectKey, index) {
        var value = multiobject[objectKey].constructor.skill.json_formatted;
        var ex = JSON.parse(value);
            edge.push(ex); 
    });
    
    console.log(edge);

    https://jsfiddle.net/v9mucpo4/

    我应该如何筛选项目? 谢谢

    1 回复  |  直到 5 天前
        1
  •  0
  •   Spectric    5 天前

    你可以利用 Array#filter .

    要过滤掉奇数值,请使用:

    pulse.filter(f => f % 2 != 0)
    

    % (模数)symbol得到除法后两个数的余数。为了检验它是否奇数,我们检查被2除后的余数是否不是0。

    演示(使用 Array#forEach 在数组中循环):

    const obj=[{irregular:{pulse:[91,0],pulse_quality:[255,0]}},{irregular:{pulse:[77,0,77,500,77,1001,77,1504,77,2007,77,2512,77,3015,77,3517,77,4018,77,4521,77,5024,77,5530,77,6033,77,6534,77,7036,77,7539,77,8043,77,8547,74,9049,74,13208,74,13741,74,14243,74,14745,79,15284,79,15827,79,16367,79,16910,79,17450,79,17991,79,18533,79,19075,79,19616,79,20157,81,20659,81,21199,81,21742,81,22282,81,22823,78,23366,78,23909,78,24449,78,24990,78,25532,80,26073,80,26616,80,27156,80,27657,80,28158,80,28699,80,29239,80,29782,80,30322,80,30865,83,31366,83,31905,83,32448,83,32949,83,33488,83,34031,82,34571,82,35115,82,35654,82,36155,82,36698,82,37237,82,37781,82,38320,82,38864,82,39403,82,39947,82,40487,82,41030,82,41570,82,42114,82,42653,82,43197,82,43736,82,44280,82,44819,82,45363,82,45902,82,46446,82,46984,82,47485,82,48029,82,48568,82,49113,82,49651,82,50195,82,50734,82,51279,82,51817,82,52360,82,52862,82,53400,82,53945,82,54483,82,54984,82,55527,82,56066,82,56612,82,57149,82,57694,82,58194,82,58733,82,59277,82,59816],pulse_quality:[255,0,255,500,255,1001,255,1504,255,2007,255,2512,255,3015,255,3517,255,4018,255,4521,255,5024,255,5530,255,6033,255,6534,255,7036,255,7539,255,8043,255,8547,255,9049,255,13208,255,13741,255,14243,255,14745,255,15284,255,15827,255,16367,255,16910,255,17450,255,17991,255,18533,255,19075,255,19616,255,20157,255,20659,255,21199,255,21742,255,22282,255,22823,255,23366,255,23909,255,24449,255,24990,255,25532,255,26073,255,26616,255,27156,255,27657,255,28158,255,28699,255,29239,255,29782,255,30322,255,30865,255,31366,255,31905,255,32448,255,32949,255,33488,255,34031,255,34571,255,35115,255,35654,255,36155,255,36698,255,37237,255,37781,255,38320,255,38864,255,39403,255,39947,255,40487,255,41030,255,41570,255,42114,255,42653,255,43197,255,43736,255,44280,255,44819,255,45363,255,45902,255,46446,255,46984,255,47485,255,48029,255,48568,255,49113,255,49651,255,50195,255,50734,255,51279,255,51817,255,52360,255,52862,255,53400,255,53945,255,54483,255,54984,255,55527,255,56066,255,56612,255,57149,255,57694,255,58194,255,58733,255,59277,255,59816]}},{irregular:{pulse:[75,0],pulse_quality:[255,0]}},{irregular:{pulse:[21,0],pulse_quality:[255,0]}},{irregular:{pulse:[99,0],pulse_quality:[255,0]}}];
    
    obj.forEach((e) => {
      e.irregular.pulse = e.irregular.pulse.filter(f => f % 2 != 0);
      e.irregular.pulse_quality = e.irregular.pulse_quality.filter(f => f % 2 == 0)
    })
    console.log(obj);