By Kevin Porche


2019-03-14 20:01:31 8 Comments

I have some data that looks like this:

[ { filename: 'file1.doc'},{ filename: 'file2.doc'} ]

What I want to do is to get rid of the surounding brackets [] so that all that's left are the objects:

{ filename: 'file1.doc'},
{ filename: 'file2.doc'}

How can I do this?

3 comments

@Rushabh Master 2019-03-14 20:27:03

If you just want to get rid of '[]' from your string then use below

var arr = "[ { filename: 'file1[[[]]].doc'},{ filenam]]]]e: 'f[[]][][][][]ile2.doc'} ]";
arr=arr.replace(/[\[\]]/g, '')
alert(arr);

@Felix Kling 2019-03-14 20:32:28

The arr.indexOf(...) checks seem unnecessary. If the character is not contained in the string, .replace won't have an effect anyway.

@Rushabh Master 2019-03-14 20:33:59

Yes, right but if you have multiple brackets then arr.indexOf(...) will change the first bracket only not all. @FelixKling

@Felix Kling 2019-03-14 20:36:47

No, .indexOf doesn't change anything, it only checks whether the substring is contained in the string. It doesn't matter whether there are multiple occurrences or not, .replace with a string literal will always only replace the first one anyway. So whether you do "if string contains [, replace first [" or just "replace first [" makes no difference. The result is the same.

@Rushabh Master 2019-03-14 20:39:25

Yes, your right I've edited code.

@Felix Kling 2019-03-14 20:42:14

The problem with .replace("]", ""); is that it will replace the first occurrence of ], not the last. So if you really have nested arrays, this will remove the wrong character. But then again, I don't know what the point of removing the outer [...] is anyway ¯\_(ツ)_/¯.

@Rushabh Master 2019-03-14 20:45:11

See this jsfiddle.net/85skfvgp/1 Maybe make sense.

@Felix Kling 2019-03-14 20:52:45

Somehow I didn't realize you have a loop. That's even weirder to be honest. If you really want to replace all occurrences of [ and ], then just doing arr.replace(/[\[\]]/g, '') would be much simpler. No need for a loop.

@Rushabh Master 2019-03-14 20:56:26

Great edited answer:) Thanks @FelixKling

@Bruce Bruce 2019-03-14 20:13:40

Like other have said, its tough to know what your goals are.

If I understand correctly, you could just use a for loop to separate the values of the array.

var arr = [ { filename: 'file1.doc'},{ filename: 'file2.doc'} ];
for(i = 0; i < arr.length; i++){
    let object = arr[i];
    console.log(object);
}

@Michel Calheiros 2019-03-14 20:06:42

A nice way would be spreading the array to a object.

let arr =[ { filename: 'file1.doc'},{ filename: 'file2.doc'} ]

let newObj = {...arr}
// or
// let newObj = Object.assign({}, arr)

console.log(newObj)

Example: https://codepen.io/anon/pen/drdaoq

@Felix Kling 2019-03-14 20:19:27

And what does that achieve exactly?

@Bruce Bruce 2019-03-14 20:23:34

yea im curious why this is the accepted answer, doesn't seem to do much of anything.

@Matthew 2019-03-14 20:24:26

All I can think is if the person wanted to lose the array and just have a JSON object instead. Else, this code literally does nothing but waste cpu and memory.

@Michel Calheiros 2019-03-14 20:27:28

console.log(arr instanceof Array) // true and console.log(newObj instanceof Array) // false so it transforms the array in a object.

@Felix Kling 2019-03-14 20:30:44

Of course, but why is that useful?

Related Questions

Sponsored Content

54 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

37 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

33 Answered Questions

[SOLVED] For-each over an array in JavaScript?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

76 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5579755 View
  • 6917 Score
  • 76 Answer
  • Tags:   javascript arrays

47 Answered Questions

26 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

35 Answered Questions

[SOLVED] How do I loop through or enumerate a JavaScript object?

47 Answered Questions

[SOLVED] Convert form data to JavaScript object with jQuery

33 Answered Questions

Sponsored Content