You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从数组筛选对象并将hexCode映射赋值到目标对象?

How to Map Hex Codes Between Datasets via Leave Type/Tag

Absolutely feasible! This is a super common data mapping task, and there are straightforward ways to pull it off depending on the language you're using. Let me walk you through how to do it with two popular options:

JavaScript Implementation

First, we'll turn your source data into a lookup map (so we can quickly find the hex code for any leave tag), then use that map to fill in the hex codes in your target dataset.

// Your original source data
const sourceData = [
  { dateCreated: "2019-10-30 08:29:54 PM", hexCode: "#ed4c67", id: 1, leaveTag: "Vacation Leave", leaveTagColor: "Bara Red" },
  { dateCreated: "2019-10-30 08:30:05 PM", hexCode: "#ea2027", id: 2, leaveTag: "Sick Leave", leaveTagColor: "Red Pigment" }
];

// Your target dataset that needs hex codes
const targetData = [
  { leaveType: "Emergency Leave", email: "sfe.bruce.logan@gmail.com", hexCode: null },
  { leaveType: "Sick Leave", email: "sfe.bruce.logan@gmail.com", hexCode: null }
];

// Step 1: Create a lookup map where keys are leave tags and values are hex codes
const hexCodeLookup = sourceData.reduce((map, item) => {
  map[item.leaveTag] = item.hexCode;
  return map;
}, {});

// Step 2: Fill in the hex codes in the target data
const updatedTargetData = targetData.map(item => ({
  ...item,
  // Use the lookup map, or keep the original value if no match is found
  hexCode: hexCodeLookup[item.leaveType] || item.hexCode
}));

// Result:
// [
//   { leaveType: "Emergency Leave", email: "...", hexCode: null },
//   { leaveType: "Sick Leave", email: "...", hexCode: "#ea2027" }
// ]

Python Implementation

The logic is identical—build a lookup dictionary first, then iterate through the target data to fill in the missing values.

# Original source data
source_data = [
    {"dateCreated": "2019-10-30 08:29:54 PM", "hexCode": "#ed4c67", "id": 1, "leaveTag": "Vacation Leave", "leaveTagColor": "Bara Red"},
    {"dateCreated": "2019-10-30 08:30:05 PM", "hexCode": "#ea2027", "id": 2, "leaveTag": "Sick Leave", "leaveTagColor": "Red Pigment"}
]

# Target dataset needing hex codes
target_data = [
    {"leaveType": "Emergency Leave", "email": "sfe.bruce.logan@gmail.com", "hexCode": None},
    {"leaveType": "Sick Leave", "email": "sfe.bruce.logan@gmail.com", "hexCode": None}
]

# Step 1: Build a lookup dictionary
hex_code_lookup = {item["leaveTag"]: item["hexCode"] for item in source_data}

# Step 2: Update the target data
updated_target_data = [
    {**item, "hexCode": hex_code_lookup.get(item["leaveType"], item["hexCode"])}
    for item in target_data
]

# Result:
# [
#   {'leaveType': 'Emergency Leave', 'email': '...', 'hexCode': None},
#   {'leaveType': 'Sick Leave', 'email': '...', 'hexCode': '#ea2027'}
# ]

Key Notes

  • If you want to handle missing matches (like "Emergency Leave" in your example), you can replace item.hexCode (or item["hexCode"] in Python) with a default hex code (e.g., #cccccc).
  • Using a lookup map is efficient—especially if your datasets are large—because it avoids looping through the entire source data for every item in the target.

内容的提问来源于stack exchange,提问作者Alyssa Reyes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:26:50