最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Select distinct objects from nested arrays using linq.js - Stack Overflow

matteradmin6PV0评论

Let's say I have the following data,

data: {
    variations: [{
        steps: [
            { Name: "Crawl", Status: "Complete" },
            { Name: "Walk", Status: "InProgress" }
        ]
    },{
        steps: [
            { Name: "Crawl", Status: "Complete" },
            { Name: "Walk", Status: "Complete" },
            { Name: "Run", Status: "NotStarted" }
        ]
    }]
}

How would I arrive at this set of data using linq.js? The resulting set of data is the unique steps across all variations. Notice, the duplicate Crawl is not in the result.

[
    { Name: "Crawl", Status: "Complete" },
    { Name: "Walk", Status: "InProgress" },
    { Name: "Walk", Status: "Complete" },
    { Name: "Run", Status: "NotStarted" }
]

I have tried many binations of Select and SelectMany, but I'm having no luck.

Let's say I have the following data,

data: {
    variations: [{
        steps: [
            { Name: "Crawl", Status: "Complete" },
            { Name: "Walk", Status: "InProgress" }
        ]
    },{
        steps: [
            { Name: "Crawl", Status: "Complete" },
            { Name: "Walk", Status: "Complete" },
            { Name: "Run", Status: "NotStarted" }
        ]
    }]
}

How would I arrive at this set of data using linq.js? The resulting set of data is the unique steps across all variations. Notice, the duplicate Crawl is not in the result.

[
    { Name: "Crawl", Status: "Complete" },
    { Name: "Walk", Status: "InProgress" },
    { Name: "Walk", Status: "Complete" },
    { Name: "Run", Status: "NotStarted" }
]

I have tried many binations of Select and SelectMany, but I'm having no luck.

Share Improve this question asked Apr 7, 2016 at 9:29 Scott LinScott Lin 1,5621 gold badge19 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

First you'll need to flatten to an array of steps. Once you have that, you'll have to pick out the distinct copies of the steps. Since you're dealing with objects, you'll need to provide a parer. I would just bine the properties that make it distinct into a string.

var query = Enumerable.From(result.data.variations)
  .SelectMany("$.steps")
  .Distinct("[$.Name, $.Status].join(',')")
  .ToArray();
Post a comment

comment list (0)

  1. No comments so far