zhangxinxu / quiz

小测答题收集区
536 stars 43 forks source link

JS基础测试43期 #61

Open zhangxinxu opened 4 years ago

zhangxinxu commented 4 years ago

本期关于实际项目中常见的数据处理。

如果图无法访问,点击这里查看原图。

大家提交回答的时候,注意缩进距离,起始位置从左边缘开始;另外,github自带代码高亮,所以请使用下面示意的格式(1积分)。

```js
// 你的JS代码写在这里
 ```

本期小测会以要点形式进行回复。

zer0fire commented 4 years ago
function getValue(data) {
var result = []
data.forEach(it => {
    if(!result.includes(it.value)) {
        result.push(it.value)
    }
})
}
return result
LuckyRabbitFeet commented 4 years ago
var value = new Set()
data.forEach(item => {
  value.add(item.value)
})
value = [...value]
console.log(value)

or

let obj = {}
let value = []
data.forEach(item => {
  if (!obj[item.value]) {
    obj[item.value] = 1
    value.push(item.value)
  }
})
smileyby commented 4 years ago
// 1. 检测data合法性
// 2. 检测value合法性
// 3. 尽可能少的循环
let getValue = (data) => {
  if (!data || !Array.isArray(data)) return []; 
  let valueArr = [];
  for (let i = 0; i < data.length; i += 1) {
    let item = data[i].value;
    if (item && valueArr.indexOf(item) < 0) {
      valueArr.push(item)
    }
  }
  return valueArr;
}
sunyongming commented 4 years ago
var valueList = []
data.forEach(function (item) {
  if (valueList.indexOf(item.value) == -1)
    valueList.push(item.value)
})
console.log(valueList)
livetune commented 4 years ago
function getValue(data) {
  return Object.keys(
    data.reduce((res, { value }) => {
      res[value] = 1;
      return res;
    }, {})
  );
}
console.log(getValue(data));
xxf1996 commented 4 years ago
let data = [
  {
    id: 1,
    name: '张三',
    value: '技术部'
  },
  {
    id: 2,
    name: '王一',
    value: '技术部'
  },
  {
    id: 3,
    name: '赵四',
    value: '客服部'
  },
  {
    id: 4,
    name: '二狗',
    value: '产品部'
  },
  {
    id: 5,
    name: '李五',
    value: '产品部'
  }
]

function getValue(info) {
  let val = new Set()
  info.forEach(item => {
    val.add(item.value)
  })

  return Array.from(val)
}

console.log(getValue(data))
Seasonley commented 4 years ago
[...data.reduce((acc,cur)=>acc.add(cur.value),new Set())]
ziven27 commented 4 years ago

var data=[{
    "id":1,
    "name":"张三",
    "value":"技术部"
},{
    "id":2,
    "name":"张三",
    "value":"技术部"
},{
    "id":3,
    "name":"张三",
    "value":"技术部"
},{
    "id":4,
    "name":"张三",
    "value":"产品部"
}];
var result=[...new Set(data.map(item => item.value))];
console.log(result);
zy017 commented 4 years ago
Array.from(new Set(data.map(o => o.value)))
gogoend commented 4 years ago
function getValues(data){
    let resultObj={};
    data.forEach(item=>{
        resultObj[item.value]='';
    })
    return Object.keys(resultObj);
}
zhangxinxu commented 4 years ago

本期小测比较基础,就是提取数组项,然后去重。

  1. ES6语法实现很简介:Array.from(new Set(data.map(o => o.value)))
  2. 传统实现:创建一个空的临时数组,遍历数组,检测有没有重复,没有重复就把遍历的值push到临时数组,最后返回这个临时数组。
guqianfeng commented 4 years ago
        let data = [
            {
                id: 1,
                name: "zhangsan",
                value: "技术部"
            },
            {
                id: 2,
                name: "lisi",
                value: "技术部"
            },
            {
                id: 3,
                name: "wangwu",
                value: "产品部"
            },
            {
                id: 4,
                name: "gqf",
                value: "测试部"
            },
            {
                id: 5,
                name: "zhaoliu",
                value: "技术部"
            },
        ]

        function filterDept(arr){
            return [...new Set(arr.map(item => item.value))]
        }

        console.log(filterDept(data))
silverWolf818 commented 4 years ago
const data = [
    {
        "id": 1,
        "name": "张三",
        "value": "技术部"
    },
    {
        "id": 2,
        "name": "李四",
        "value": "产品部"
    },
    {
        "id": 3,
        "name": "王二",
        "value": "测试部"
    },
    {
        "id": 4,
        "name": "王五",
        "value": "技术部"
    }
];

const arr1 = [...new Set(data.map(i => i.value))];
const arr2 = Array.from(new Set(data.map(i => i.value)));

console.log(arr1)
console.log(arr2)
NeilChen4698 commented 4 years ago
[...new Set(data.map(v => v.value))]
rayj1993 commented 4 years ago
let data = [{
    'id': 1,
    'name': '张三',
    'value': '技术部',
}, {
    'id': 2,
    'name': '李四',
    'value': '技术部',
}, {
    'id': 3,
    'name': '王五',
    'value': '产品部',
}]

function getKey(key) {
    let arr = []
    data.forEach((item) => {
        arr.push(item[key])
    })
    return Array.from(new Set(arr))
}
console.log(getKey('value'))
wingmeng commented 4 years ago

ES6 方式

const result = [...new Set(data.map(item => item.value))];

ES5 方式

var result = data.reduce(function(arr, item) {
  return !~arr.indexOf(item.value) ? arr.concat(item.value) : arr
}, []);