如何根据值获取密钥而不重复

时间:2017-09-23 09:38:32

标签: javascript jquery

我有一种情况,我想只获得密钥和时间确定i,e ampm

这是我的数据

    var  amUserDate = {"monday|am":false,"tuesday|am":true,"wednesday|am":false,"thursday|am":false,"friday|am":false,"saturday|am":true,"sunday|am":false};

   var   pmUserDate = {"monday|pm":true,"tuesday|pm":false,"wednesday|pm":false,"thursday|pm":false,"friday|pm":false,"saturday|pm":false,"sunday|pm":true};

我想要的输出:

基于true {值}的key => value对结果将如下所示

amAvailibility  = [{"day":"tuesday","time":"am"},{"day":"saturday","time":"am"}];


pmAvailibility  = [{"day":"monday","time":"pm"},{"day":"sunday","time":"pm"}];

请提前帮助我!!!!

3 个答案:

答案 0 :(得分:1)

您可以使用此代码来实现您的目标:



 var  amUserDate = {"monday|am":false,"tuesday|am":true,"wednesday|am":false,"thursday|am":false,"friday|am":false,"saturday|am":true,"sunday|am":false};

var pmUserDate = {"monday|pm":true,"tuesday|pm":false,"wednesday|pm":false,"thursday|pm":false,"friday|pm":false,"saturday|pm":false,"sunday|pm":true};

var amKeys = Object.keys(amUserDate),
    pmKeys = Object.keys(pmUserDate),
    amAvailibility = [],
    pmAvailibility = [];

amKeys.forEach(function(key, index){
   var obj = {};
   if(amUserDate[key]){
     var splitStr = key.split('|');
     obj = {day: splitStr[0], time: splitStr[1] }
     amAvailibility.push(obj);
   }
});

pmKeys.forEach(function(key, index){
   var obj = {};
   if(pmUserDate[key]){
     var splitStr = key.split('|');
     obj = {day: splitStr[0], time: splitStr[1] }
     pmAvailibility.push(obj);
   }
});

console.log(amAvailibility);
console.log(pmAvailibility);




答案 1 :(得分:0)

使用for...in迭代对象并使用if条件将对象推送到新数组。

var  amUserDate = {"monday|am":false,"tuesday|am":true,"wednesday|am":false,"thursday|am":false,"friday|am":false,"saturday|am":true,"sunday|am":false};

var   pmUserDate = {"monday|pm":true,"tuesday|pm":false,"wednesday|pm":false,"thursday|pm":false,"friday|pm":false,"saturday|pm":false,"sunday|pm":true};

function transformObject(obj){
   var arr =[];
   for(var key in obj){
      var time = key.replace(/.*\|/,"");
      if(obj[key])
        arr.push({"date": key.replace(/\|.*/, ""), "time":time});
   }

   return arr;
}

var amAvailibility = transformObject(amUserDate), pmAvailibility = transformObject(pmUserDate);

console.log(amAvailibility);
console.log(pmAvailibility);

答案 2 :(得分:0)

这是一种方法。



var amUserDate = {"monday|am":false,"tuesday|am":true,"wednesday|am":false,"thursday|am":false,"friday|am":false,"saturday|am":true,"sunday|am":false};
var pmUserDate = {"monday|pm":true,"tuesday|pm":false,"wednesday|pm":false,"thursday|pm":false,"friday|pm":false,"saturday|pm":false,"sunday|pm":true};

function fn(obj){
    return Object.keys(obj)
           .filter(key => obj[key])
           .reduce((acc, key) => {
              var splittedKey = key.split("|");
              acc.push({day:splittedKey[0], time:splittedKey[1]});
              return acc;
          }, []);
}

var am = fn(amUserDate);
var pm = fn(pmUserDate);

console.log(am);
console.log(pm)