0

I have a json like this (there will be many obj like):

const products = [
  {
    "COMUNE": "Alghero",
    "PROVINCIA": "SASSARI",
    "REGIONE": "Sardegna",
    "Codice scuola": "SSCT70100N",
    "Denominazione dell'istituto": "SEBASTIANO SATTA",
    "Tipologia istituto - denominazione": "SCUOLA SECONDARIA DI SECONDO GRADO",
    "Indirizzo (Via + numero civico)": "VIA SASSARI 80",
    "Codice Postale": "07041",
    "Telefono": "0790981003",
    "Fax": "",
    "Email": "SSMM08100P@istruzione.it",
    "Email - PEC": "",
    "Sito web": "",
    "Istituto principale di riferimento (sede direzione)": "SSMM08100P",
    "Statale/Paritaria": "STATALE"
  },...

I need to delete everything but "Denominazione dell'istituto" so eventually the json would be:

{
"Denominazione dell'istituto" "lorem"
},
{
"Denominazione dell'istituto": "ipsum"
},...

I tried:

console.log(products.map(obj => delete obj["Codice scuola"] && obj));

But I don't know how I would delete more than one obj at once.

Also, in reality, I need to generate a series of results I could copy paste like, let's say that after we remove all we have

"Denominazione dell'istituto": "SEBASTIANO SATTA",
"Denominazione dell'istituto": "FRANK WHITE",
"Denominazione dell'istituto": "JAMES WONDER",

I'd be looking at a plain:

SEBASTIANO SATTA
FRANK WHITE
JAMES WONDER
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • Or directly see [From an array of objects, extract value of a property as array](https://stackoverflow.com/q/19590865/4642212) to map your entries to values. You don’t need to remove all other keys beforehand if you just want the list of values. Simply map the objects to that one property value. – Sebastian Simon May 28 '20 at 02:41
  • @user4642212 yes thanks a lot, followed both of them and works fine – rob.m May 28 '20 at 02:47

3 Answers3

1

To delete the properties:

// Walk through products array
for (var i = 0; i < products.length; ++i) {
    var obj = products[i];

    // Walk through object properties
    for (var key in obj) {
        if (key !== "Denominazione dell'istituto") {
            // delete the property
            delete obj[key];
        }
    }
}

To print them:

for (var i = 0; i < products.length; ++i) {
    var obj = products[i];

    for (var key in obj) {
        console.log(obj["Denominazione dell'istituto"])
    }
}

However you don't really want to delete things from a const variable.

It would be a better idea to just extract the information from the object and put it into an array like this:

var result = [];

for (var i = 0; i < products.length; ++i) {
    for (var key in products[i]) {
        if (key === "Denominazione dell'istituto") {
            result.push(products[i][key]);
        }
    }
}

console.log(result);
Marco
  • 7,007
  • 2
  • 19
  • 49
  • _“However you don't really want to delete things from a `const` variable.”_ — Why not? `const` neither means nor implies _immutability_. – Sebastian Simon May 28 '20 at 02:59
  • @user4642212 I disagree: `const is a type qualifier:[a] a keyword applied to a data type that indicates that the data is read only` — wikipedia about the `const` keyword. – Marco May 28 '20 at 03:04
  • That’s not how it’s defined in JavaScript. Even “read only” doesn’t imply immutable. If I have a constant object with a property, that object reference never changes (read only), but the property value still may change, as the value is not part of the identity of the object. – Sebastian Simon May 28 '20 at 03:08
  • In any case, not deleting properties (not mutating the object) is good practice, but it’s unrelated to `const`. – Sebastian Simon May 28 '20 at 03:09
1

Returning a new object in your .map would be much simpler than mutating the previous values.

const output = (products.map(obj => ({
  "Denominazione dell'istituto": obj["Denominazione dell'istituto"]
})));
console.log(output)

If you'd like to print the values out for the second part of your question,

let outputString = '';
output.forEach(value => {
  outputString = `${outputString}${value["Denominazione dell'istituto"]}\n`
});
console.log(outputString); // This will be what you need to copy
Lim Jing Rong
  • 426
  • 2
  • 4
1

If you only need one specific property from an array of objects. You don't have to mutate the objects themselves. Just making an array of those values sounds good enough:

const array = products.map(product => product["Denominazione dell'istituto"]);
console.log(array.join('\n'));

This will print

SEBASTIANO SATTA
FRANK WHITE
JAMES WONDER
...
Hao Wu
  • 17,573
  • 6
  • 28
  • 60
  • 1
    Thanks a lot, accepting this as it's one line elegant piece of code. – rob.m May 28 '20 at 02:51
  • actually look at this https://jsfiddle.net/p1votudw/ it gives wrong results, not only showing that obj values – rob.m May 28 '20 at 02:53