-1

I am sure there is a clean way to do this, but I have no idea how to do it. I want to pluck a column out such that I am only returning the first occurrence of a value, but I want to keep the key that went with it.

I have a dataset that I want reduced. I want to pluck out the 'precip'.

Say I have this:

[
  "2019-01-01" => {"temp" : "cold", "season" : "winter", "precip" : "snow"},
  "2019-02-01" => {"temp" : "cold", "season" : "winter", "precip" : "none"},
  "2019-03-01" => {"temp" : "mild", "season" : "spring", "precip" : "rain"},
  "2019-04-01" => {"temp" : "mild", "season" : "spring", "precip" : "none"},
  "2019-05-01" => {"temp" : "warm", "season" : "spring", "precip" : "rain"},
  "2019-06-01" => {"temp" : "warm", "season" : "summer", "precip" : "hail"},
  "2019-07-01" => {"temp" : "hot", "season" : "summer", "precip" : "none"}
]

I would like to end up with this:

[
  "2019-01-01" => "snow",
  "2019-02-01" => "none",
  "2019-03-01" => "rain",
  "2019-06-01" => "hail"
]

I would think that Array.map has something to do with this, but I don't know how to return the key/value pair instead of just a value (i.e. map(function(d) { return d.precip }) )

What is the smooth way to do this?

Thanks in advance.

4
  • what do you mean with map? an array or a Map? Commented Jul 30, 2019 at 17:51
  • Note that it's hard to tell exactly what you are working with since the syntax is invalid. Now have 2 answers both making different assumptions Commented Jul 30, 2019 at 17:57
  • I apologize. In the title, I was referring to a Map (object). In the text, I was referencing the array map function. Commented Jul 30, 2019 at 18:04
  • 2
    please add a valid data structure/type for data and result Commented Jul 30, 2019 at 18:11

2 Answers 2

3

You could create a Map and take only the first item with the same key.

function getFirstPrecip(data) {
    return Object.assign({}, ...Array.from(
        data.reduce((m, o) => {
            var [[k, { precip }]] = Object.entries(o);
            return m.has(precip) ? m : m.set(precip, k);
        }, new Map),
        ([k, v]) => ({ [v]: k })
    ));
}

var data = [{ "2019-01-01": { temp: "cold", season: "winter", precip: "snow" } }, { "2019-02-01": { temp: "cold", season: "winter", precip: "none" } }, { "2019-03-01": { temp: "mild", season: "spring", precip: "rain" } }, { "2019-04-01": { temp: "mild", season: "spring", precip: "none" } }, { "2019-05-01": { temp: "warm", season: "spring", precip: "rain" } }, { "2019-06-01": { temp: "warm", season: "summer", precip: "hail" } }, { "2019-07-01": { temp: "hot", season: "summer", precip: "none" } }];

console.log(getFirstPrecip(data));

Sign up to request clarification or add additional context in comments.

1 Comment

So I'm trying to get this to work with my code, and am having trouble. I noticed you have a comma after the var data declaration, and then go straight into the code. How would you change it if this code were in a function, and data was an array passed as an argument?
0

You can iterate the keys for the main object, and create a new object that will use those keys and assign the precip attribute value as value for the new key=>value pair:

var dates = {
  "2019-01-01" : {"temp" : "cold", "season" : "winter", "precip" : "snow"},
  "2019-02-01" : {"temp" : "cold", "season" : "winter", "precip" : "none"},
  "2019-03-01" : {"temp" : "mild", "season" : "spring", "precip" : "rain"},
  "2019-04-01" : {"temp" : "mild", "season" : "spring", "precip" : "none"},
  "2019-05-01" : {"temp" : "warm", "season" : "spring", "precip" : "rain"},
  "2019-06-01" : {"temp" : "warm", "season" : "summer", "precip" : "hail"},
  "2019-07-01" : {"temp" : "hot", "season" : "summer", "precip" : "none"}
};
var result = Object.keys(dates).reduce((k,d) => {return [...k, {[d]:dates[d].precip}]}, [])
console.log(result)

3 Comments

I have a question that makes this a bit harder. What if the date was inside the key-value pairs (say "date":"2019-07-01"), and I wanted the result to be the same? So how would I pull the date out of the key-value pair and make it the new key for the returned dataset?
You'd just have to replace the {[d]:dates[d].precip}, where d is the date key, for {[dates[d].date]:dates[d].precip} to use the new key extracted from another value in the object.
You can try the above code by replaceing [d] for [dates[d].temp] to get a key:value pair from the temp and precip values in your object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.