JavaScript Remove keys from object array and turn values into key value pairs remove keys from dictionary python,remove keys from keyboard,remove

I have an array like the following:

data =
   [{'province': 'North', 'level': 1, 'count': 204},
    {'province': 'South', 'level': 1, 'count': 24},
    {'province': 'East', 'level': 1, 'count': 24},
    {'province': 'West', 'level': 1, 'count': 39}];

I want to remove the attribute level, and want to make the province value a key, and the count value the value making the desired output:

result = 
   {'North':204, 'South':24, 'East':24, 'West':39}

I have been able to remove the 'level attribute with the following:

let test = _.map(data, function(currentObject) {
    return _.pick(currentObject, "province", "count");
  });

which gives me:

test = 
    [{'province': 'North', 'count': 204},
    {'province': 'South', 'count': 24},
    {'province': 'East', 'count': 24},
    {'province': 'West', 'count': 39}];

How can I get to my desired result?

Answer:1

This can be done with Array.reduce():

let data =[
  {'province': 'North', 'level': 1, 'count': 204},
  {'province': 'South', 'level': 1, 'count': 24},
  {'province': 'East', 'level': 1, 'count': 24},
  {'province': 'West', 'level': 1, 'count': 39}
];

let res = data.reduce((acc, curr) =>
{
    acc[curr.province] = curr.count;
    return acc;
}, {});

console.log(res);
Answer:2

Given that I understood what the desired out come is, map the data into a new object:

const data =
      [{'province': 'North', 'level': 1, 'count': 204},
       {'province': 'South', 'level': 1, 'count': 24},
       {'province': 'East', 'level': 1, 'count': 24},
       {'province': 'West', 'level': 1, 'count': 39}];

const provinces = data.map(d => ({ [d.province]: d.count }));

console.log(provinces)
Answer:3

So I'm creating a basic website, and I used the typewriter function from w3schools: https://www.w3schools.com/howto/howto_js_typewriter.asp My problem is that I want to display the text on 2 lines, ...

So I'm creating a basic website, and I used the typewriter function from w3schools: https://www.w3schools.com/howto/howto_js_typewriter.asp My problem is that I want to display the text on 2 lines, ...

When I use the document.write function to print the number in the code below, it inserts 0 to the right side of each output. Why is this happening? The code works as expected for the alert function. ...

When I use the document.write function to print the number in the code below, it inserts 0 to the right side of each output. Why is this happening? The code works as expected for the alert function. ...

  1. when to use document.write
  2. when would you use document.write()

After some research the following suggestion by Mr. Evan You was found: https://github.com/vuejs/vue/issues/7349#issuecomment-354937350 So without any hesitation I gave it a try: Component template ...

After some research the following suggestion by Mr. Evan You was found: https://github.com/vuejs/vue/issues/7349#issuecomment-354937350 So without any hesitation I gave it a try: Component template ...

Currently, I use PrestaShop, and I wanted to change the width of search box, but, when I change the width of search box to 650px, on desktop it's ok, but on mobile the search box is very long — not ...

Currently, I use PrestaShop, and I wanted to change the width of search box, but, when I change the width of search box to 650px, on desktop it's ok, but on mobile the search box is very long — not ...