JSR如何双层数据对象获取对应关系
场景:我有两个对象数据:分别是:_defaultPrivilegeIcont
和 userConfig
需要实现的效果:
1、_defaultPrivilegeIcont
可以通过userConfig
的awardListVO[xx][xx]item
和levelCode
赋值到_defaultPrivilegeIcont
的describe
变成动态的赋值的效果;例如:_defaultPrivilegeIcont[0].list[0].describe = 'VIP1可享'
其中VIP1
取的就是的userConfig
的item
,然后拿到的levelCode
关系;
2、如果同时多个等级匹配中 需要获取最低的那个;比如:userConfig.awardListVO
里面的item
有多个1
那么_defaultPrivilegeIcont[0].list.describe
就显示VIP1
获取的是等级最低的那个levelCode
实现了很多的方式都无法得到理想的状态;请知道的大佬帮忙看一下
// 目标数据
const _defaultPrivilegeIcont = [
{
title: "平台特权",
type: 1,
list: [
{
levelCode: 1,
defaultIcont: "portrait_v1",
activeIcont: "portrait_v2",
title: "权益头像框",
describe: "",
english: "Profile photo frame",
content: "",
lock: false,
tips: "",
id: "v-1",
item: 1,
layer: {
title: "权益头像框",
isLock: false,
benefitText:
"vip专享,每达到新1级VIP等级,可解锁对应精美头像框。",
tips: [],
isBirthday: false,
btnText: "",
},
},
{
levelCode: 3,
defaultIcont: "coupons_v1",
activeIcont: "coupons_v2",
title: "升级奖励券",
english: "Coupons after upgrades",
describe: "",
lock: false,
tips: "50%Discount",
content: "",
id: "v-2",
item: 2,
layer: {
title: "升级奖励劵",
isLock: false,
benefitText:
"vip3级及以上可享,每达到新1级VIP等级,可获得1张对应的无门槛优惠券。VIP等级越高,优惠券面值越高。",
tips: ["*每一等级的优惠券每位用户只能领取一次"],
isBirthday: false,
btnText: "查看优惠券",
},
},
{
levelCode: 4,
defaultIcont: "kd_v1",
activeIcont: "kd_v2",
title: "升级送KGold",
english: "K-coins after upgrades",
describe: "",
lock: false,
tips: "100k-Gold",
content: "",
id: "v-3",
item: 3,
layer: {
title: "升级送KGold",
isLock: false,
benefitText:
"vip4级及以上可享,每达到新1级VIP等级,即可直接获得对应的KGOLD奖励。VIP等级越高,奖励的Kgold越多。",
tips: [
"*每一等级的KGold奖励,每位用户只能享受一次",
"*Kgold可用于购买平台所有商品,方便快捷。",
],
isBirthday: false,
btnText: "查看KGold",
},
},
{
levelCode: 9,
defaultIcont: "birthday_v1",
activeIcont: "birthday_v2",
title: "生日福利",
english: "Birthday gift",
describe: "",
content: "",
lock: false,
tips: "",
id: "v-4",
item: 4,
layer: {
title: "生日福利",
isLock: false,
benefitText:
"svip专享,一年仅可领一次。可联系svip专属客服了解详情。",
tips: ["*生日日期仅能修改一次"],
isBirthday: true,
btnText: "填写生日",
},
},
{
levelCode: 9,
defaultIcont: "pleasantly_v1",
activeIcont: "pleasantly_v2",
title: "神秘惊喜",
english: "Surprise gift",
describe: "",
content: "",
lock: false,
tips: "",
id: "v-5",
item: 5,
layer: {
title: "神秘惊喜",
isLock: false,
benefitText:
"svip3/4/5专享,周期惊喜礼物。您的svip专属客服将主动联系您,告知详情。",
tips: "",
isBirthday: false,
btnText: "",
},
},
],
},
{
title: "服务特权",
type: 2,
list: [
{
levelCode: 2,
defaultIcont: "fast_v1",
activeIcont: "fast_v2",
title: "绿色客服通道",
english: "Dedicated customer support",
describe: "",
content: "",
lock: false,
tips: "",
id: "v-6",
item: 6,
layer: {
title: "绿色客服通道",
isLock: false,
benefitText:
"vip2级及以上可享,优先排队,问题被优先处理,减少等待时间。",
tips: "",
isBirthday: false,
btnText: "",
},
},
{
levelCode: 7,
defaultIcont: "exclusive_v1",
activeIcont: "exclusive_v2",
title: "SVIP专属经理",
english: "SVIP personal manager",
content: "",
describe: "",
lock: false,
tips: "",
id: "v-7",
item: 7,
layer: {
title: "SVIP专属经理",
isLock: false,
benefitText:
"svip专享,专属客服将主动联系您,问题快速响应,1对1服务。",
tips: "",
isBirthday: false,
btnText: "",
},
},
],
},
{
title: "游戏特权",
type: 3,
list: [
{
levelCode: 9,
defaultIcont: "prop_v1",
activeIcont: "prop_v2",
english: "In-game items",
title: "游戏道具申请",
describe: "",
content: "",
lock: false,
tips: "",
id: "v-8",
item: 8,
layer: {
title: "游戏道具申请",
isLock: false,
benefitText:
"svip专享,可在充值前,联系svip专属客服提出需求。为您单独申请特殊奖励。",
tips: ["*请在充值前联系"],
isBirthday: false,
btnText: "",
},
},
],
},
];
const userConfig = [ {
country: "ph",
levelCode: 0,
levelOrder: 1,
startSpendingExp: 0,
endSpendingExp: 10,
awardList: [],
awardListVO: [],
},
{
country: "ph",
levelCode: 1,
levelOrder: 2,
startSpendingExp: 10,
endSpendingExp: 100,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP1-1169.jpg",
},
],
},
],
},
{
country: "ph",
levelCode: 2,
levelOrder: 3,
startSpendingExp: 100,
endSpendingExp: 500,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP2170.jpg",
},
],
},
{ type: 2, itemList: [{ item: 6 }] },
],
},
{
country: "ph",
levelCode: 3,
levelOrder: 4,
startSpendingExp: 500,
endSpendingExp: 1000,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP3172.jpg",
},
{ item: 3, content: "8" },
],
},
{ type: 2, itemList: [{ item: 6 }] },
],
KGoldCount: 8,
},
{
country: "ph",
levelCode: 4,
levelOrder: 5,
startSpendingExp: 1000,
endSpendingExp: 2500,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP4173.jpg",
},
{ item: 3, content: "10" },
],
},
{ type: 2, itemList: [{ item: 6 }] },
],
KGoldCount: 10,
},
{
country: "ph",
levelCode: 5,
levelOrder: 6,
startSpendingExp: 2500,
endSpendingExp: 10000,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP5174.jpg",
},
{ item: 3, content: "30" },
],
},
{ type: 2, itemList: [{ item: 6 }] },
],
KGoldCount: 30,
},
{
country: "ph",
levelCode: 6,
levelOrder: 7,
startSpendingExp: 10000,
endSpendingExp: 25000,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP6176.jpg",
},
{ item: 3, content: "150" },
],
},
{ type: 2, itemList: [{ item: 6 }] },
],
KGoldCount: 150,
},
{
country: "ph",
levelCode: 7,
levelOrder: 8,
startSpendingExp: 25000,
endSpendingExp: 50000,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP1178.jpg",
},
{ item: 3, content: "300" },
],
},
{ type: 2, itemList: [{ item: 6 }, { item: 7 }] },
],
KGoldCount: 300,
},
{
country: "ph",
levelCode: 8,
levelOrder: 9,
startSpendingExp: 50000,
endSpendingExp: 150000,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP2-1180.jpg",
},
{ item: 3, content: "500" },
],
},
{ type: 2, itemList: [{ item: 6 }, { item: 7 }] },
],
KGoldCount: 500,
},
{
country: "ph",
levelCode: 9,
levelOrder: 10,
startSpendingExp: 150000,
endSpendingExp: 250000,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP3-1182.jpg",
},
{ item: 2, content: "537" },
{ item: 3, content: "800" },
{ item: 4 },
{ item: 5 },
],
},
{ type: 2, itemList: [{ item: 6 }, { item: 7 }] },
{ type: 3, itemList: [{ item: 8 }] },
],
couponIds: ["537"],
KGoldCount: 800,
},
{
country: "ph",
levelCode: 10,
levelOrder: 11,
startSpendingExp: 250000,
endSpendingExp: 500000,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP4-1183.jpg",
},
{ item: 2, content: "538,544" },
{ item: 3, content: "1000" },
{ item: 4 },
{ item: 5 },
],
},
{ type: 2, itemList: [{ item: 6 }, { item: 7 }] },
{ type: 3, itemList: [{ item: 8 }] },
],
couponIds: ["538", "544"],
KGoldCount: 1000,
},
{
country: "ph",
levelCode: 11,
levelOrder: 12,
startSpendingExp: 500000,
awardList: [],
awardListVO: [
{
type: 1,
itemList: [
{
item: 1,
content:
"/wp-content/uploads/new2024/02/20240204vue12345/SVIP5-1184.jpg",
},
{ item: 2, content: "539" },
{ item: 3, content: "2500" },
{ item: 4 },
{ item: 5 },
],
},
{ type: 2, itemList: [{ item: 6 }, { item: 7 }] },
{ type: 3, itemList: [{ item: 8 }] },
],
couponIds: ["539"],
KGoldCount: 2500,
},
];
回答:
几个疑问
(1) 示例中的 VIP1可享
压根没出现在数据中啊,所以这个信息从哪里来?
(2) 我分析了一下 userConfig 中 levelCode 和 item 的关系,结果如下
[ { levelCode: 0, items: [] },
{ levelCode: 1, items: [ 1 ] },
{ levelCode: 2, items: [ 1, 6 ] },
{ levelCode: 3, items: [ 1, 3, 6 ] },
{ levelCode: 4, items: [ 1, 3, 6 ] },
{ levelCode: 5, items: [ 1, 3, 6 ] },
{ levelCode: 6, items: [ 1, 3, 6 ] },
{ levelCode: 7, items: [ 1, 3, 6, 7 ] },
{ levelCode: 8, items: [ 1, 3, 6, 7 ] },
{ levelCode: 9, items: [ 1, 2, 3, 4, 5, 6, 7, 8 ]},
{ levelCode: 10, items: [ 1, 2, 3, 4, 5, 6, 7, 8 ]},
{ levelCode: 11, items: [ 1, 2, 3, 4, 5, 6, 7, 8 ]}
]
确实存在一个 item 对应着多个 levelCode 的情况,但是 _defaultPrivilegeIcont.list
中的每一项都有 levelCode 值,为什么会需要按 item 去找最小的 levelCode?
(3) 算是一个建议:要找到 (2) 中描述的关系其实很容易,可以倒转过来建建立一个 item 到多个 levelCode 的映射,这时候要先找 item,再去找对应的最小 levelCode 就很容易了
以上是 JSR如何双层数据对象获取对应关系 的全部内容, 来源链接: utcz.com/p/937464.html