Skip to main content
deleted 28 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

create Create list of objects from an object where the key is properCase using lodashLodash

I'm trying to create a filtered list of objects where each object in the list has both a label and value property.

First I use .pick to select the key/value pairs I want from the initial object, then I iterate over the 'picked' properties and push the key into an object property called label, and push the value of the property into a property called value. I then return the empty array.

function formatToInput(obj, list) {
    var empty = []

    _.forIn(_.pick(obj, list), (val, key) => {
        empty.push({
            label: _.startCase(key),
            value: val
        });
    });

    return empty;
}

// use like so

var test = {blue: 10, green: 5, yellow: 3, pink: 1};
var filter = ['green', 'yellow'];

var results = formatToInput(test, filter)

console.log(results);

// [{label: Green, value: 5}, {label: Yellow, value: 3}]

TryingI'm trying to avoid having to manually create an array and pushing objects into it, but I couldn't find a solution (in lodashLodash) that would allow me to do this.

Any feedback would be great.

create list of objects from an object where the key is properCase using lodash

I'm trying to create a filtered list of objects where each object in the list has both a label and value property.

First I use .pick to select the key/value pairs I want from the initial object, then I iterate over the 'picked' properties and push the key into an object property called label, and push the value of the property into a property called value. I then return the empty array.

function formatToInput(obj, list) {
    var empty = []

    _.forIn(_.pick(obj, list), (val, key) => {
        empty.push({
            label: _.startCase(key),
            value: val
        });
    });

    return empty;
}

// use like so

var test = {blue: 10, green: 5, yellow: 3, pink: 1};
var filter = ['green', 'yellow'];

var results = formatToInput(test, filter)

console.log(results);

// [{label: Green, value: 5}, {label: Yellow, value: 3}]

Trying to avoid having to manually create an array and pushing objects into it, but I couldn't find a solution (in lodash) that would allow me to do this.

Any feedback would be great.

Create list of objects from an object where the key is properCase using Lodash

I'm trying to create a filtered list of objects where each object in the list has both a label and value property.

First I use .pick to select the key/value pairs I want from the initial object, then I iterate over the 'picked' properties and push the key into an object property called label, and push the value of the property into a property called value. I then return the empty array.

function formatToInput(obj, list) {
    var empty = []

    _.forIn(_.pick(obj, list), (val, key) => {
        empty.push({
            label: _.startCase(key),
            value: val
        });
    });

    return empty;
}

// use like so

var test = {blue: 10, green: 5, yellow: 3, pink: 1};
var filter = ['green', 'yellow'];

var results = formatToInput(test, filter)

console.log(results);

// [{label: Green, value: 5}, {label: Yellow, value: 3}]

I'm trying to avoid having to manually create an array and pushing objects into it, but I couldn't find a solution (in Lodash) that would allow me to do this.

Source Link
Nick Blexrud
  • 550
  • 1
  • 8
  • 22

create list of objects from an object where the key is properCase using lodash

I'm trying to create a filtered list of objects where each object in the list has both a label and value property.

First I use .pick to select the key/value pairs I want from the initial object, then I iterate over the 'picked' properties and push the key into an object property called label, and push the value of the property into a property called value. I then return the empty array.

function formatToInput(obj, list) {
    var empty = []

    _.forIn(_.pick(obj, list), (val, key) => {
        empty.push({
            label: _.startCase(key),
            value: val
        });
    });

    return empty;
}

// use like so

var test = {blue: 10, green: 5, yellow: 3, pink: 1};
var filter = ['green', 'yellow'];

var results = formatToInput(test, filter)

console.log(results);

// [{label: Green, value: 5}, {label: Yellow, value: 3}]

Trying to avoid having to manually create an array and pushing objects into it, but I couldn't find a solution (in lodash) that would allow me to do this.

Any feedback would be great.