gpt4 book ai didi

javascript - 编写一个函数对对象数组进行排序(通过使用另一个对象来指定排序路径和顺序)

转载 作者:数据小太阳 更新时间:2023-10-29 06:04:06 24 4
gpt4 key购买 nike

我将编写一个函数来对具有某种结构的任何 JSON 进行排序(只要知道它是一个对象数组,例如产品列表),方法是使用另一个对象作为其参数以确定根据哪个对象执行排序键。

// The json that I get might looks like something like this.
// I just write one item of the array, but all of them are the same.
// But the blueprint of the items in each json are different.
const dataArray = [
{
id: 100,
name: 'product_1',
price: 99.95,
color: ['#fff', '#f0f', '#00f'],
category: ['cat_1', 'cat_2'],
booleanKey: true,
foo: {
foo1: 12,
foo2: 'string value',
foo3: {
foo3_1: 'string value',
foo3_2: 732.342
},
foo4: [
{
foo4_1_1: 853,
foo4_1_2: 24,
},
{
foo4_2_1: 'string value',
foo4_2_2: 435,
foo4_2_3: 25.35,
}
]
},
bar: {
bar1: {
bar1_1: 313,
bar1_2: 32.73
},
bar2: 75.3
}
}
];

// The function to sort the array
const sortData = obj => {
return dataArray.sort((a, b) => {
// Do something to return the correct value
});
}

// Sort array ascending by value 1, and descending by value -1
const result = sortData({foo: {foo3: {foo3_2: 1}}});
const anotherResult = sortData({bar: {bar2: -1}});
const anotherResult = sortData({price: 1});

因为我必须编写一个函数来对具有任何结构的任何项目进行排序,所以我必须使用它们传递给我的 obj 作为参数来根据该对象的该键对数组进行排序。

我找到了这个解决方案:

// The sample array that be used from the other answer
const fooArray = [
{ foo1: { foo2: { foo3: 123 } } },
{ foo1: { foo2: { foo3: 987 } } },
{ foo1: { foo2: { foo3: 456 } } },
{ foo1: { foo2: { foo3: 789 } } },
{ foo1: { foo2: { foo3: 321 } } },
{ foo1: { foo2: { foo3: 654 } } }
];
const sortData = obj => {
const pathArray = [];
while(typeof(obj) == 'object') {
pathArray.push(Object.keys(obj)[0]);
obj = obj[pathArray[pathArray.length - 1]];
}
const order = obj;
return fooArray.sort((a, b) => {
for(let pathKey of pathArray) {
a = a[pathKey];
b = b[pathKey];
}
return (a - b) * order;
});
}
const foo = sortData({ foo1: { foo2: { foo3: 1 } } });

还有其他建议或想法吗?

最佳答案

对于排序逻辑,很简单:

const sortData = path => {
const sortOrder = query(path, path)

return dataArray.sort((a, b) => {
const valueOfA = query(a, path)
const valueOfB = query(b, path)

if (valueOfA < valueOfB) return -sortOrder
if (valueOfA > valueOfB) return sortOrder
return 0
})
}

这里唯一复杂的是如何从另一个对象类型的参数中查询排序路径和顺序。

所以我在这里定义了一个辅助函数来完成这项工作:

function query (target, path) {
const [key] = Object.keys(path)

if (typeof path[key] !== 'object') {
return target[key]
}

return query(path[key], target)
}

你可以在这里测试:

let dataArray = [{id: 100}, {id: 200}]

const sortData = path => {
const sortOrder = query(path, path)

return dataArray.sort((a, b) => {
const valueOfA = query(a, path)
const valueOfB = query(b, path)

if (valueOfA < valueOfB) return -sortOrder
if (valueOfA > valueOfB) return sortOrder
return 0
})
}

console.log(sortData({id: 1})) // 100, 200
console.log(sortData({id: -1})) // 200, 100

dataArray = [
{foo: {bar: 'bar'}, a:'a', b:'b'},
{foo: {bar: 'car'}, a:'A', b:'B'}
]

console.log(sortData({foo: {bar: 1}})) // [...bar, ...car]
console.log(sortData({foo: {bar: -1}})) // [...car, ...bar]

function query (target, path) {
const [key] = Object.keys(path)

if (typeof path[key] !== 'object') {
return target[key]
}

return query(path[key], target)
}

关于javascript - 编写一个函数对对象数组进行排序(通过使用另一个对象来指定排序路径和顺序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55913794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com