-2

I have a JSON as below.

{
"key" : "Balance",
"translation" : [ 
    {
        "language" : "English",
        "value" : "abc"
    }, 
    {
        "language" : "German",
        "value" : "faq"
    }
]
}

I'm passing the language "English" and the language and its value should delete. There are many keys so I'm using for loop in my function to get that language.

this._work.forEach( ( translation ) => {
        translation.translation.forEach( data => delete data.language[ language ] );
    } );

this._work contains the above json value. Here the language and its value not deleting. What I'm missing?

Kirataka
  • 484
  • 8
  • 26
  • 1
    _"I have a JSON as below"_ - No, that's not [JSON](https://www.json.org/json-en.html). What you have there is an object -> [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas May 22 '20 at 08:36

3 Answers3

1

In your solution, delete data.language[ language ] will not work since data.language['English'] is undefined. This deletion technique is generally used when you want to remove by using key from an object, not row in an array.

You can use filter instead of using forEach.

this._work.forEach( ( row ) => {
   row.translation = row.translation.filter( translation => translation.language !== translation );
} );

1

Aside note: There is nothing called JSON en js, what you have is a js object.

Actually you're trying to delete the attribute "English" from the string language, which is wrong and doesn't make sense.

For example:

delete data.language["English"]

I recommend you to use the function Array.prototype.filter as follow:

let data = {
  "key": "Balance",
  "translation": [{
      "language": "English",
      "value": "abc"
    },
    {
      "language": "German",
      "value": "faq"
    }
  ]
},
    language = "English";
    
data.translation = data.translation.filter(({language: lang}) => lang !== language);
console.log(data);
    
Ele
  • 33,468
  • 7
  • 37
  • 75
  • Is there anything specific to assign in flower bracket? – Kirataka May 22 '20 at 10:02
  • @Kirataka sorry, I didn't understand. – Ele May 22 '20 at 10:24
  • u have declared this `({language: lang})` in flower braces. Is it for a specific reason? – Kirataka May 22 '20 at 11:01
  • 1
    @Kirataka this is just to use a destructuring assignment of the property language, there is not a reason, it's just for a fancy approach, otherwise, in the handler of the function `filter` you have to access the property as follow: `object.language` – Ele May 22 '20 at 11:06
0
{
   property: 'value',
   language: 'English'
}

delete instruction needs a property provided to be deleted. like delete data.property or delete data.language. In the code, you provided data.language[language] points to a value. If you want to delete the language property if the value is English, then the code you needs is

this._work.forEach( ( translation ) => {
        translation.translation.forEach( data => if (language === data.language) { delete data.language; });
    } );

as a result you would get:

{
"key" : "Balance",
"translation" : [ 
    {
        "value" : "abc"
    }, 
    {
        "language" : "German",
        "value" : "faq"
    }
]
}
IAfanasov
  • 4,775
  • 3
  • 27
  • 42