有如下数据结构。
e test1
b test2
c test2
i test3
d test3
f test3
k test3
h test3
左边是字段1(值),右边是字段2(属性名)
把属性名进行去重,然后把左侧的值归并到指定的属性名下,如下格式:
[
{
category: 'test1',
arr: ['e']
},
{
category: 'test2',
arr: ['b','c']
},
{
category: 'test3',
arr: ['i','d','k','f','h']
}
]
我的思路,遍历数组,已经拿到了属性值的数组,形如
[
{
category: 'test1'
},
{
category: 'test2'
},
{
category: 'test3'
}
].
有啥思路方法获取同属性数组然后放到对应的字段名下?
JavaScript的数据结构包括数组、对象、Set、Map、箭头函数、模板字符串、函数、箭头函数、类等。
javascript
let array = [1, 2, 3, 4, 5];
console.log(array[0]); // 输出:1
javascript
let obj = {
name: 'John',
age: 30,
city: 'New York'
};
console.log(obj.name); // 输出:John
console.log(obj.age); // 输出:30
console.log(obj.city); // 输出:New York
javascript
let set = new Set();
set.add('red');
set.add('blue');
console.log(set.size); // 输出:2
javascript
let map = new Map();
map.set('name', 'John');
map.set('age', 30);
console.log(map.get('name')); // 输出:John
console.log(map.get('age')); // 输出:30
javascript
let sum = (a, b) => a + b;
console.log(sum(2, 3)); // 输出:5
javascript
let name = 'John';
let age = 30;
console.log(`My name is ${name} and I am ${age} years old.`);
javascript
let sayHello = (name) => {
console.log(`Hello, ${name}!`);
};
sayHello('World');
javascript
let multiply = (a, b) => a * b;
console.log(multiply(2, 3)); // 输出:6
javascript
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
let person = new Person('John', 30);
person.sayHello();
以上就是JavaScript中的一些基本数据结构,但JavaScript的数据结构远不止这些。