import subprocess
import shlex
command = "ls -lh compute.py | awk '{print $5}'"
# the command returns the following results on native bash
# 2.7K
args = shlex.split(command)
p = subprocess.Popen(args,executable="/bin/bash", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
print p.communicate()
# the results run with subprocess.Popen:
# ('1\n11.bin\n1.bin\n1.log\n1.sql\n2.bin\ncompute.py\n','')
# it sounds like subprocess.Popen just run `ls`
在这里,我试图在react.js中的json上方进行映射。 但是其中一些是根据NS键重复的。
删除重复项后如何映射。
请看看
答案 0 :(得分:2)
来源:https://dev.to/vuevixens/removing-duplicates-in-an-array-of-objects-in-js-with-sets-3fep
var x = [
{
"NS": "food",
"Brand": "bosch",
"legendKey": "5_1"
},
{
"NS": "food",
"Brand": "bosch",
"legendKey": "5_1"
},
{
"NS": "performance",
"Brand": "ge",
"legendKey": "4_2"
}
];
var unique = Array.from(new Set(x.map(a => a.NS))).map(NS => x.find(a => a.NS === NS));
console.log(unique)
答案 1 :(得分:0)
您可以进行过滤以获取唯一值,然后呈现数据
<ul class="legend">Needstates
{
this.state.legend ? this.state.legend.filter((item , index, arr) => arr.findIndex(obj => obj.NS === item.NS) == index).map((ele, i) =>{
return(
<li>
<span key={i} style={{backgroundColor:this.state.colors[ele['legendKey'][2]-1]}}></span> {ele['NS']}
</li>
)
}): null
}
</ul>
答案 2 :(得分:0)
解决方案1::您可以更改属性必须检查其唯一性的keys
变量。
const keys = ['NS', 'Brand', 'legendKey']
const filtered = this.state.legend.filter(
(s => o =>
(k => !s.has(k) && s.add(k))
(keys.map(k => o[k]).join('|'))
)
(new Set)
);
console.log(filtered);
输出:
[{NS:'food',品牌:'bosch',legendKey:'5_1'}, {NS:'performance',Brand:'ge',legendKey:'4_2'}]
答案 3 :(得分:0)
让我们调用您的原始数据json
和唯一项unique
。我们将通过评估是否已使用 Brand 和 NS 来检查重复项。
let json = [{
"NS": "food",
"Brand": "bosch",
"legendKey": "5_1"
},
{
"NS": "food",
"Brand": "bosch",
"legendKey": "5_1"
},
{
"NS": "performance",
"Brand": "ge",
"legendKey": "4_2"
}]
let unique = []
json.forEach((item) => {
var i = unique.findIndex((uniqueItem) => {
return uniqueItem.NS == item.NS && uniqueItem.Brand == item.Brand
})
if(i < 0){
unique.push(item)
}
})
console.log(unique)
保持整洁,以跟踪发生的事情。
[]
的空unique
。json
,对于每个item
,我们将检查内部
unique
使用NS和找到匹配的uniqueItem
的索引
品牌为参数。将该索引存储在名为i的变量中。json
,现在,如果找到匹配的项目,我们
将获得大于等于0的索引,这意味着其重复项,
所以我们对此不做任何事情。答案 4 :(得分:0)
简单的例子:
let things= [{
"NS": "food",
"Brand": "bosch",
"legendKey": "5_1"
},
{
"NS": "food",
"Brand": "bosch",
"legendKey": "5_1"
},
{
"NS": "performance",
"Brand": "ge",
"legendKey": "4_2"
}];
let result = [...new Set(things.map(s => JSON.stringify(s)))].map(s => JSON.parse(s));
console.log(result)