2

Which is the easiest way to convert this:

[{src:"websrv1"}, {dst:"websrv2"}, {dstport:"80"}]

to this:

{src:"websrv1", dst:"websrv2", dstport:"80"}

in order to pass it to AJAX data?

I'm using VisualSearch and it returns an array of Facet model instances which i need to convert into an Object.

asked Mar 6, 2014 at 20:02
14
  • Are the parentheses ( and ) supposed to be curly braces { and }? Commented Mar 6, 2014 at 20:04
  • Why are there parenthesis (()) in your desired object? Commented Mar 6, 2014 at 20:05
  • 1
    You can just pass the array, $.ajax accepts arrays as well. Commented Mar 6, 2014 at 20:06
  • 1
    $.extend.apply({}, [{src:"websrv1"}, {dst:"websrv2"}, {dstport:"80"}]) Commented Mar 6, 2014 at 20:13
  • Why are people giving jQuery solutions? I don't see any jQuery here, or am I just missing it? Commented Mar 6, 2014 at 20:15

6 Answers 6

3
var a = [{src:"websrv1"}, {dst:"websrv2"}, {dstport:"80"}];
 var b = a.reduce(
 function(reduced,next){
 Object.keys(next).forEach(function(key){reduced[key]=next[key];});
 return reduced;
 }
 );
//b should be {src:"websrv1", dst:"websrv2", dstport:"80"}

think about the array.reduce function everytime you need to perform these kind of transformations.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

answered Mar 6, 2014 at 20:09
Sign up to request clarification or add additional context in comments.

1 Comment

I really just can't figure it out. Although it should work, visualsearch returns only the last Facet model instance on each search.
2

If you are using jquery, try this:

var array = [{src:"websrv1"}, {dst:"websrv2"}, {dstport:"80"}]
var arrayObj = {};
for(var i in array) {
 $.extend(arrayObj, array[i]);
}
answered Mar 6, 2014 at 20:06

Comments

1

Use .reduce().

var result = data.reduce(function(obj, item) {
 for (var key in item)
 obj[key] = item[key];
 return obj;
}, {});
answered Mar 6, 2014 at 20:08

Comments

0

Don't use this! but just for fun

var a = [{src:"websrv1"}, {dst:"websrv2"}, {dstport:"80"}];
var f = a.reduce((c,d) => Object.assign(c,d), {})

The tiny drawback is that a is mutated with an infinite recursive object but, who cares? it works in one line!

answered Feb 21, 2018 at 11:46

Comments

-1

My 2cents, very easy to read:

var myObj = {};
myArray.forEach(function(obj) {
 var prop = Object.keys(obj)[0];
 myObj[prop] = obj[prop];
})
answered Mar 6, 2014 at 20:27

Comments

-1

Original answer using only the most basic features of JavaScript:

var input = [{src:"websrv1"}, {dst:"websrv2"}, {dstport:"80"}];
var output = {};
for (var i = 0; i < input.length; i++) {
 for (var n in input[i]) {
 output[n] = input[i][n];
 }
}
console.log(output);


UPDATE: Using newer features of JavaScript, you can do this trivially with Object.assign and spread syntax (...):

var input = [{src:"websrv1"}, {dst:"websrv2"}, {dstport:"80"}];
var output = Object.assign({}, ...input);
console.log(output);

Also, Object.assign(...input) will return the same result, but will modify the first element of the input array. As long as you don't mind that side effect, I'd use this simpler version.

answered Mar 6, 2014 at 20:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.