4

I have a JSON array:

var arr = [
{ID: "1", Title: "T1", Name: "N1"}, 
{ID: "2", Title: "T2", Name: "N2"}, 
{ID: "3", Title: "T3", Name: "N3"}
]

How can I delete the Title key from all the rows without a loop cycle?

Result should look like:

var arr = [
{ID: "1", Name: "N1"}, 
{ID: "2", Name: "N2"}, 
{ID: "3", Name: "N3"}
]

I tried the following:

delete arr.Title

but it results a logical response "true" instead of an array.

3
  • 2
    That's a wrong js object, you meant array []? Commented Mar 11, 2019 at 23:56
  • Yes, array [{}, ... ] will edit the question Commented Mar 12, 2019 at 0:01
  • 1
    processing an array is by it's very nature an iterative task. Whether it's explicit in a for loop or implicit in a map or reduce operation, those are all iterative, or what you are calling loop cycle. Why is it necessary to not to use some sort of iteration? Unless this is purely academic, then fine. One other thing, JSON is a string representation of an Object. What you have is an object literal or an array of object literals. The true result is not very helpful. delete {}.abcdefg will return true as well. Commented Mar 12, 2019 at 0:28

6 Answers 6

5

you weren't so far:

[edit] with alternatives solutions Combine 1) forEach / for..of. 2) delete / Reflect.deleteProperty

let
  arr_1 = [
    {ID: 1, Title: "T1", Name: "N1"}, 
    {ID: 2, Title: "T2", Name: "N2"}, 
    {ID: 3, Title: "T3", Name: "N3"}
  ],
  arr_2 = arr_1.map(e=>Object.assign({},e))  // new array of copies
;

// solution 1
arr_1.forEach(elm=>delete elm.Title)

// solution 2
for(let elm of arr_2){ Reflect.deleteProperty(elm, 'Name') } // changing

console.log('arr_1 =', JSON.stringify(arr_1))
console.log('arr_2 =', JSON.stringify(arr_2))

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

3 Comments

Not sure why only this version works in JIVE html tile? even this case I had to re-write in jQuery like: *** var jQuery.each(arr, function(elm, value){ delete value.Title; }); *** Other suggestions only work in jsFiddle.
forEach is ECMAScript 5 full JS interpretors => caniuse.com/#search=forEach // I have change my code to show alternetive solution
Just to point out that I've clarified my code, but I'm wondering if it useful for JIVE?
3

An alternative would be the function map to create a new array with the desired output. Inside of the handler, create a new Object using the object from each index and finally delete the undesired property name Title.

Assuming you meant array, this approach doesn't mutate the original objects.

let arr = [{ID: "1", Title: "T1", Name: "N1"}, {ID: "2", Title: "T2", Name: "N2"}, {ID: "3", Title: "T3", Name: "N3"}],
    result = arr.map(o => {
      let obj = Object.assign({}, o);
      delete obj.Title;
      return obj;
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

4 Comments

This seems to be the correct solution so far, could you assess if this requires the least resorce to run?
@HCMan: what sort of resources are you trying to minimize? And why?
I am working in JIVE and running AJAX takes 4/5 second and I would not increase response time
Somehow the Object.assign({}, o) is not working with IE, I tried to use this suggestion as well with no success: stackoverflow.com/questions/35215360/…
1

You want to do this without a loop. I'm not sure what you mean. We can certainly write code that doesn't explicitly use for in order to loop, if that's what you want. But JS, like most languages (there are exceptions such as APL, and K), does not offer any way to operate directly on the elements of the list en masse. So, you can abstract the looping with map. But there is still likely some looping under the hood.

var arr = [
  {ID: "1", Title: "T1", Name: "N1"}, 
  {ID: "2", Title: "T2", Name: "N2"}, 
  {ID: "3", Title: "T3", Name: "N3"}
]

const newArr = arr.map(({Title, ...rest}) => ({...rest}))

console.log(newArr)

2 Comments

Why not using map(omit('Title')) ;)
@IslamAttrash: Plenty of libraries include omit functions, and I personally use Ramda's, but there is little obvious reason to bring one in here, unless you expect to reuse it.
1

For actual JSON string, the JSON.parse reviver parameter can be used to filter or modify values :

var json = '[{"ID":"1","Title":"T1","Name":"N1"},{"ID":"2","Title":"T2","Name":"N2"},{"ID":"3","Title":"T3","Name":"N3"}]'

var arr = JSON.parse(json, (k, v) => k != 'Title' ? v : void 0);

console.log( arr );

2 Comments

Why are you returning k[-1]? I suppose that you have to return undefined in this case
@EduMelo it's just a shortcut for undefined as undefined can be set to any value
0

Your variable arr is not an array. It is an object.
Objects are surrounded by curly braces {}, like this: { "name":"Bob", "alive":true, "dead":false }
An array is surrounded by brakets [], like this: ["Bob","Jane","Mary]

2 Comments

My answer is no longer applicable. The question has been edited.
Got it and corrected - question still same thanks for the heads up
0

You can accomplish the solution using functional utilities that give you to solve the things in easier way.

import { map, omit } from 'lodash/fp';
const newArr = map(omit('Title'), arr);

1 Comment

Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. From Review

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.