根据其他键值获取数组中出现的no

时间:2018-03-01 17:03:48

标签: javascript reactjs lodash

int i = 0;
while(true) {

 std::thread t1(function1);
 std::thread t2(function2);

 t1.detach();
 t2.detach();

//also break your infinite loop here
if( ++i < 4)
   break;
}

我有上面的数组,我必须构建对象,基于&#34;类型&#34;价值即&#34;预定义&#34;或&#34; custom&#34;如下

var json = [{
        "city": "California",
        "name": "Joe",
        "age": 17,
        "type",:"custom"
    }, {
        "city": "California",
        "name": "Bob",
        "age": 17,
        "type",:"predefined"
    }, {
        "city": "California",
        "name": "Bob",
        "age": 35,
        "type",:"custom"
    }, {
        "city": "Texas",
        "name": "Bob",
        "age": 35,
        "type",:"custom"
    }, {
        "city": "Florida",
        "name": "Bob",
        "age": 35,
        "type",:"predefined"
 }];

任何使用javascript或lodash的方法

3 个答案:

答案 0 :(得分:0)

您可以使用array.reduce创建包含聚合数据的新对象。例如:

  const result = json.reduce((obj, data) => {
      // if this is a new type, we must initialize it as an empty array and push the first record.
      if(!obj[data.type]) {
        obj[data.type] = [];
        obj[data.type].push({name: data.city, count: 1});
        return obj;
      }
      // else ... check if the current city is present in that type
      const existingRecord = obj[data.type].find(d => d.name === data.city);
      if(!existingRecord) {
         // if the city is not present in that type, we initialize it with count 1
         obj[data.type].push({name: data.city, count: 1});
         return obj;
      }
      // if the city is present, we just update count = count + 1;
      existingRecord.count++;
      return obj;
    }, { })

现在,您可能希望将某些逻辑提取到单独的函数中以提高可读性。我不认为你需要lodash,但是如果你已经把它包含在你的项目中,那么随时可以使用它:P

答案 1 :(得分:0)

  

使用Lodash

&#13;
&#13;
var json = [{
  "city": "California",
  "name": "Joe",
  "age": 17,
  "type": "custom"
}, {
  "city": "California",
  "name": "Bob",
  "age": 17,
  "type": "predefined"
}, {
  "city": "California",
  "name": "Bob",
  "age": 35,
  "type": "custom"
}, {
  "city": "Texas",
  "name": "Bob",
  "age": 35,
  "type": "custom"
}, {
  "city": "Florida",
  "name": "Bob",
  "age": 35,
  "type": "predefined"
}];

// Solution to the problem

var updatedJson = _(json).groupBy("type").value();    // #1

_.forOwn(updatedJson, function(value, key) {
  let countByCity = _(value).countBy('city').value(); // #2
  let res = [];

  _.forOwn(countByCity, function(value, key) {
    res.push({                                        // #3
      name: key,
      count: value
    });
  });

  updatedJson[key] = res;
});

console.log(updatedJson);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
&#13;
&#13;
&#13;

  

解释

  1. 按类型编码第一组
  2. 然后按城市内的数量计算
  3. 最后将count对象推送到一个数组中,格式为

答案 2 :(得分:0)

您可以使用reduce&amp; findIndex

var json = [{
  "city": "California",
  "name": "Joe",
  "age": 17,
  "type": "custom"
}, {
  "city": "California",
  "name": "Bob",
  "age": 17,
  "type": "predefined"
}, {
  "city": "California",
  "name": "Bob",
  "age": 35,
  "type": "custom"
}, {
  "city": "Texas",
  "name": "Bob",
  "age": 35,
  "type": "custom"
}, {
  "city": "Florida",
  "name": "Bob",
  "age": 35,
  "type": "predefined"
}];

var updatedjson = json.reduce(function(result, item) {
  // check if type is present in the object
  // if not present then add a key by name custom/predefined
  if (!result[item.type]) {
    result[item.type] = [];
    // add city name and intial count
    result[item.type].push({
      name: item.city,
      count: 1
    })
  } else {
    // now find the index of the object whe name matches with current city
    let m = result[item.type].findIndex(function(obj) {
      return obj.name === item.city
    })
    // if no matches then add the new object to either custom/predefined
    if (m === -1) {
      result[item.type].push({
        name: item.city,
        count: 1
      })
    } else {
      // if matches then increase the value of count
      result[item.type][m].count += 1
    }

  }
  return result;

}, {});

console.log(updatedjson)