使用JSON数据获取特定颜色的特定值 [英] Getting specific color for specific value using JSON data

查看:135
本文介绍了使用JSON数据获取特定颜色的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想标识传入的数据,并且需要对相同的颜色应用完全相同的颜色。

I would like to identity incoming data and needs to apply exactly the same color for the same.

数据顺序是固定的(但是所有可能,并且需要应用如下所述的固定颜色。

Data sequence is in fixed order (but all may be present or not) and need to apply fixed color mentioned like below.

例如预期的数据顺序和颜色顺序如下:

For e.g. expected data sequence and color sequence is like below:

CLOSED           GREEN
VERIFIED         GREEN
RESOLVED         YELLOW
REOPENED         RED
IN_PROGRESS      BLUE
ASSIGNED         BROWN
NEW              BROWN
UNCONFIRMED      BROWN

因此,每当 CLOSED 出现 GREEN 颜色时,应使用已解决-> 黄色,并.... 未确认-> BROWN

So whenever CLOSED comes GREEN color should be applied, for RESOLVED -> YELLOW and .... for UNCONFIRMED -> BROWN.

现在问题是所有数据可能都会出现,例如

Now the problem is all data may come and may not come for e.g.

CLOSED
VERIFIED
IN_PROGRESS
UNCONFIRMED

用Java脚本编写的基本逻辑,例如:

So written basic logic in Java script like:

if ( jsonData.search("CLOSED") != -1 
   && jsonData.search("VERIFIED") != -1 
   && jsonData.search("IN_PROGRESS") != -1 
   && jsonData.search("UNCONFIRMED") != -1 ) {

   colors: ['green', 'green', 'blue', 'brown'],

} else if (jsonData.search("IN_PROGRESS")
   && jsonData.search("ASSIGNED") != -1 ) {

   colors: ['blue', 'brown'],

} else if (jsonData.search("IN_PROGRESS")) {

   colors: ['blue'],

} else if (jsonData.search("CLOSED") != -1 
   || jsonData.search("VERIFIED") != -1) {

   colors: ['green'],
}

这将适用于上述顺序,但是只要数据不同,例如

This will work for above sequence but whenever data would be different for e.g.

IN_PROGRESS
NEW

OR

RESOLVED
REOPENED
NEW

OR

CLOSED
IN_PROGRESS

OR

RESOLVED
NEW

等。许多组合...

然后,这将失败并将错误的颜色应用于数据。如果我写每个数据以及每个序列的条件,那么我会发疯,它也完全无效,乏味且非常复杂。

Then this will fail and apply wrong color to data. If i write condition of each and every data and respective sequence then i will become mad and its completely ineffective, tedious and very complex too.

{"cols":[{"label":"status","type":"string"},{"label":"count","type":"string"}],"rows":[{"c":[{"v":"VERIFIED"},{"v":16}]},{"c":[{"v":"RESOLVED"},{"v":1}]},{"c":[{"v":"IN_PROGRESS"},{"v":14}]},{"c":[{"v":"ASSIGNED"},{"v":39}]},{"c":[{"v":""},{"v":0}]}]}

{"cols":[{"label":"status","type":"string"},{"label":"count","type":"string"}],"rows":[{"c":[{"v":"CLOSED"},{"v":3}]},{"c":[{"v":"VERIFIED"},{"v":13}]},{"c":[{"v":"RESOLVED"},{"v":2}]},{"c":[{"v":"IN_PROGRESS"},{"v":26}]},{"c":[{"v":"ASSIGNED"},{"v":2}]},{"c":[{"v":""},{"v":0}]}]}

{"cols":[{"label":"status","type":"string"},{"label":"count","type":"string"}],"rows":[{"c":[{"v":"IN_PROGRESS"},{"v":3}]},{"c":[{"v":"NEW"},{"v":8}]},{"c":[{"v":""},{"v":0}]}]}

{"cols":[{"label":"status","type":"string"},{"label":"count","type":"string"}],"rows":[{"c":[{"v":"RESOLVED"},{"v":12}]},{"c":[{"v":"IN_PROGRESS"},{"v":9}]},{"c":[{"v":"ASSIGNED"},{"v":15}]},{"c":[{"v":""},{"v":0}]}]}

{"cols":[{"label":"status","type":"string"},{"label":"count","type":"string"}],"rows":[{"c":[{"v":"VERIFIED"},{"v":6}]},{"c":[{"v":"IN_PROGRESS"},{"v":40}]},{"c":[{"v":"ASSIGNED"},{"v":7}]},{"c":[{"v":"NEW"},{"v":8}]},{"c":[{"v":""},{"v":0}]}]}

{"cols":[{"label":"status","type":"string"},{"label":"count","type":"string"}],"rows":[{"c":[{"v":"CLOSED"},{"v":3}]},{"c":[{"v":"VERIFIED"},{"v":35}]},{"c":[{"v":"RESOLVED"},{"v":15}]},{"c":[{"v":"IN_PROGRESS"},{"v":92}]},{"c":[{"v":"ASSIGNED"},{"v":63}]},{"c":[{"v":"NEW"},{"v":16}]},{"c":[{"v":""},{"v":0}]}]}

有人可以考虑有效的逻辑吗?而不是像上面提到的那样为单个序列写单独的数据+颜色?

Can someone thinks about effective logic rather than writing individual data+color for exact sequence like mentioned?

请让我知道是否有任何问题,或者我错过了任何事情

Please let me know if any questions or i missed anything.

推荐答案

考虑到数据序列是固定顺序的,并且您将JSON视为String,我建议采用以下解决方案:

Taking into account that data sequence is in fixed order and you treat JSON as String, I'd suggest the following solution:

const DataMap = {
  CLOSED: 'green',
  VERIFIED: 'green',
  RESOLVED: 'yellow',
  REOPENED: 'red',
  IN_PROGRESS: 'blue',
  ASSIGNED: 'brown',
  NEW: 'brown',
  UNCONFIRMED: 'brown'
};

let colors = [];

Object.keys(DataMap).forEach((key, index) => {
  if (jsonData.search(key) !== -1) {
    colors.push(DataMap[key]);
  }
});

请查看 jsFiddle

这篇关于使用JSON数据获取特定颜色的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆