gpt4 book ai didi

javascript - 如何根据值获取键而不重复

转载 作者:行者123 更新时间:2023-11-30 21:08:28 25 4
gpt4 key购买 nike

我有一种情况,我只想获得 key 及其时间确定,即 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(value) 的 key => value 对结果将如下所示

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


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

请提前帮助我谢谢!!!

最佳答案

你可以使用这段代码来实现你想要的:

 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);

关于javascript - 如何根据值获取键而不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46378164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com