uniq两个数组对象并优先使用true值

时间:2017-04-25 09:58:02

标签: javascript arrays underscore.js lodash

我正在尝试在数组对象中进行数组操作,如下所示:

var first = [{
    a: "one",
    x: false
}, {
    a: "two",
    x: true
}, {
    a: "three",
    x: false
}, {
    a: "one",
    x: true
}, {
    a: "two",
    x: true
}, {
    a: "four",
    x: false
}];

预期结果:

// Result
[{
    a: "one",
    x: true
}, {
    a: "two",
    x: true
}, {
    a: "three",
    x: false
}, {
    a: "four",
    x: false
}];

正如您所看到的,我试图省略具有相同值a的重复项,但在省略该对象之前,我想比较其他重复项并推送具有真值的对象对于密钥x如果没有可用的真值,那么我将只推送x: false)。

我愿意使用lodashunderscore来实现此目标。

我尝试使用_.unionBy_.uniqBy,但我无法理解如何正确考虑x: true

3 个答案:

答案 0 :(得分:1)

使用lodash。

按属性a分组。然后合并每个组内的对象;只保留真正的价值观。



let array = [
    {a: 'one', x: false},
    {a: 'two', x: true},
    {a: 'three', x: false},
    {a: 'one', x: true},
    {a: 'two', x: true},
    {a: 'four', x: false},
];

let result = _(array)
    .groupBy('a')
    .map(objects => _.mergeWith(...objects, (a, b) => a || b))
    .value();

console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

var results = [];

first.forEach(f => {
   var index = results.map(r => r.a).indexOf(f.a);

   if(index === -1) {
      results.push(f)
   } else {
      f.x && (results[index].x = true);
   }
})

答案 2 :(得分:0)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <div class="form-group">
                          <button type="button" id="more_fields" class="btn btn-danger" onclick="add_fields();"><span class="glyphicon glyphicon-fire"></span> click me</button>
                      </div>


                      <div class="form-group">
                          <div id="room_fileds">
                              <div>
                                  <label>how the hell do i add a remove button for each new generated input?</label>
                                  <div class="content">

                                      <?php
                                      function not_empty_string($s) {
                                          return $s !== " ";
                                      }
                                      $pieces = array_filter(explode(",", $test_stingu),'not_empty_string');

                                      foreach ($pieces as $piece) {
                                          $test_stinga = array_filter(explode("-", $piece),'not_empty_string');
                                          // scoate spatiul de la inceputul stringului
                                          $test_stinga=preg_replace('/\s+/', '', $test_stinga);
                                         ?>
                                          
                                          <p class="form-inline">Test: <select class="form-control" name="test[]" style="width:70px;"> <option>Test1</option> </select>
                                          <span></span><span>Number: <input class="form-control" type="number" style="width:70px;" name="number[]" value="<?php print $test_stinga[1] ?>"/></span><button>
                                          Button
                                          </button>
                                          </p>

                                              <br>
                                      
<?php

                    
                                          }

                                          ?>


                                  </div>
                              </div>
                          </div></div>
                  <script>
                      var room = 1;
                      var wrapper         = $(".testtt");
                      function add_fields() {
                          room++;
                          var objTo = document.getElementById('room_fileds')
                          var divtest = document.createElement("div");
                          divtest.innerHTML = '<div class="testtt"><div class="label">TEST ' + room + ':</div><div class="content"><span><p class="form-inline">Testing: <select class="form-control" name="test[]" style="width:70px;"> <option value="Test">TEst</option> </select> </span><span>Number: <input class="form-control" type="number" style="width:70px;" name="number[]" value="" /><button class="remove">Remove</button></p></span></div><div>';

                          objTo.appendChild(divtest)
                      }

                      
                      
                      $('#room_fileds').on("click",".remove", function(e){
                     
                          $(this).parents().eq(5).remove();
                      })




                  </script>