在数组中对对象进行分组的最有效方法是什么?
例如,给定此对象数组:
[
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" …Run Code Online (Sandbox Code Playgroud) 我正在研究一个问题,我必须将一个对象数组从一个表单分组到另一个表单.
一个例子优于1000字:
var initialData = [
{
house: { id: 1, text: "white" },
room: { id: 1, text: "red" },
price: 2.1
},
{
house: { id: 1, text: "white" },
room: { id: 2, text: "blue" },
price: 3.1
},
{
house: { id: 1, text: "white" },
room: { id: 3, text: "red" },
price: 5.8
},
{
house: { id: 2, text: "black" },
room: { id: 1, text: "yellow" },
price: 9.1
},
{
house: …Run Code Online (Sandbox Code Playgroud) 如果值不存在,需要将数组对象(arr1)的值推送到另一个数组对象(arr2)中。现有值不会推入另一个数组。
var arr1 = [{
name: 'fred'
}, {
name: 'bill'
}, {
name: 'ted'
}, {
name: 'james'
}];
var arr2 = [{
name: 'spil'
}, {
name: 'fred'
}, {
name: 'bill'
},{
name: 'paul'
}, {
name: 'stone'
}];
function add(name) {
var found = arr1.some(function (el) {
return el.name === name;
});
if (!found) {
arr1.push(arr2);
}
return arr2;
}
Run Code Online (Sandbox Code Playgroud)
如何通过对象键对对象数组进行分组,以根据分组创建新的对象数组?例如,我有一系列汽车对象:
const array = [
{red: [ {height: 50} ]},
{green: [ {height: 20} ]},
{blue: [ {height: 30} ]},
{blue: [ {height: 40} ]},
{red: [ {height: 10} ]},
{green: [ {height: 60} ]}
]
Run Code Online (Sandbox Code Playgroud)
我想创建一个新的对象数组。(关键是颜色)
const result = [
{red: [{height: 50}, {height: 10}]},
{green: [{height: 20}, {height: 60}]},
{blue: [{height: 30}, {height: 40}]}
]
Run Code Online (Sandbox Code Playgroud)
我尝试使用 lodash.groupBy,但我根本不知道如何解决这个问题。
我的数组是这样的:
myArray = [
{date: "2017-01-01", num: "2"}
{date: "2017-01-02", num: "3"}
{date: "2017-02-04", num: "6"}
{date: "2017-02-05", num: "15"}
]
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
myArray = [
{group: "0", data: [
{date: "2017-01-01", num: "2"}
{date: "2017-01-02", num: "3"}]
},
{group: "1", data: [
{date: "2017-02-04", num: "6"}
{date: "2017-02-05", num: "15"}]
},
]
Run Code Online (Sandbox Code Playgroud)
基本上,按月日期键分组。
I am trying to dynamically split an array of objects into groups based on values of a property.
Here is an example of the input:
`input = [
{"name": "john", "location": "first"},
{"name": "steve", "location": "first"},
{"name": "paul", "location": "another"},
{"name": "tony", "location": "random"},
{"name": "ant", "location": "random"}
]`
Run Code Online (Sandbox Code Playgroud)
and the desired output:
`solution(input, location) = [
first: [{"name": "john", "location": "first"},
{"name": "steve", "location": "first"}],
another: [{"name": "paul", "location": "another"}],
random: [{"name": "tony", "location": "random"},
{"name": "ant", "location": "random"}] …Run Code Online (Sandbox Code Playgroud) 我有两个带有各种键/值的对象的 Javascript 数组。
我正在尝试使用从每个原始数组中选择的键/值来实现一个新的对象数组,但在特定键/值上具有唯一性。
例子:
const startDate = [
{
name: 'John', //Don't need this
Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', //This should be the 'unique' key
datePosted: '2020-04-04T00:01:20.000Z' //This will be the start date
}
]
const endDate = [
{
name: 'James', //Don't need this
Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', //This should be the 'unique' key
datePosted: '2021-04-04T00:01:20.000Z' //This will be the end date
}
]
const desiredOutput = [
{
'ae570d88-809b-45b1-bc20-69b569e361ce': {
startDate: '2020-04-04T00:01:20.000Z',
endDate: '2021-04-04T00:01:20.000Z'
}
}
]
const desiredOutput2 = [
{ …Run Code Online (Sandbox Code Playgroud) 我收到带有对象的数据/数组.每个对象都有国家 - 城市和商店(在那个城市).
例如:
意大利 - 罗马 - shopXY
var data = [
{country: "Italy", city: "Milan", shop: "shop123"},
{country: "Italy", city: "Rome", shop: "shop2"},
{country: "USA", city: "Los Angeles", shop: "shopXY"}
]
Run Code Online (Sandbox Code Playgroud)我有3列,我想只展示一次意大利,然后当我点击它时,我展示米兰和罗马,然后根据我是否点击罗马或米兰相应的商店.
以下是好的做法:
我试图研究,但真的找不到任何东西,即使有人只是有一些提示,这将是伟大的,因为我完全失去了.谢谢!!!!
PS.我不想使用任何过滤器库,因为我自己尝试这样做.
var dictA = { male: 10, female: 20, unassigned: 30 };
var dictB = { male: 11, female: 21, unassigned: 31 };
var dictC = { male: 12, female: 22, unassigned: 32 };
Run Code Online (Sandbox Code Playgroud)
有没有比多个循环更简单的方法来产生如下结果:
{
male: [10, 11, 12],
female: [20, 21, 22],
unassigned: [30, 31, 32]
}
Run Code Online (Sandbox Code Playgroud)
我不确定这里的“组合”是否正确。
javascript ×9
arrays ×4
object ×3
dictionary ×1
filtering ×1
group-by ×1
grouping ×1
list ×1
lodash ×1
logic ×1
reactjs ×1
typescript ×1