Before join you need map array
const array = [
{ a: '1', b: '2' },
{ a: '3', b: '4' },
];
const result = array.map(_ => _.a).join(', ');
console.log(result);
Answer from Nikita Madeev on Stack OverflowHelp with handling JSON array of objects (merge two arrays)
merge two object arrays with Angular 2 and TypeScript? - Stack Overflow
Perform .join on value in array of objects
jquery - Merge two json/javascript arrays in to one array - Stack Overflow
Before join you need map array
const array = [
{ a: '1', b: '2' },
{ a: '3', b: '4' },
];
const result = array.map(_ => _.a).join(', ');
console.log(result);
In order to perform this through For loop.
result = [];
const array = [{ a: '1', b: '2' }, { a: '3', b: '4' }];
array.forEach(elm => result.push(elm.a));
console.log(result.join(", "));
// 1, 3
I have a json array of Objects and need to pick a corresponding object in that array according to some calculations.
My json have a following structure: {"OD": number, "SCH" {"SCH40": number, "SCH80": number}}
I need to do some math with OD and SCH numbers, compare it with an outside number and find corresponding value for OD key.
I can get a new array (of numbers I compare the outside number with):
this.Pipeareas = this.Pipesizes.map (x => Math.pow((x.OD - 2 * x.SCH[this.selectedSCH])/2000,2) * Math.PI);
How to merge existing and new arrays?
I tried (this add extra existing array and new object to new array (not key:value pair))
this.Pipeareas = {...this.Pipesizes, Area: this.Pipesizes.map (x => Math.pow((x.OD - 2 * x.SCH[this.selectedSCH])/2000,2) * Math.PI) }
and (this adds new key "Area" to each object but the value is full array, not corresponding numbers from new array)
this.Pipesizes.map (x => x.Area = this.Pipesizes.map (x => Math.pow((x.OD - 2 * x.SCH[this.selectedSCH])/2000,2) * Math.PI))
The result should be like:
return this.Pipeareas.find(x => x >= this.OutsideNumber);
The spread operator is kinda cool.
this.results = [ ...this.results, ...data.results];
The spread operator allows you to easily place an expanded version of an array into another array.
You can read about spread operator here.
I think that you should use rather the following:
data => {
this.results = this.results.concat(data.results);
this._next = data.next;
},
From the concat doc:
The concat() method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.
If you want to map objects to something (in this case a property). I think Array.prototype.map is what you're looking for if you want to code functionally.
(fiddle)
If you want to support older browsers, that are not ES5 compliant you can shim it (there is a polyfill on the MDN page above). Another alternative would be to use underscorejs's pluck method:
var users = [
{name: "Joe", age: 22},
{name: "Kevin", age: 24},
{name: "Peter", age: 21}
];
var result = _.pluck(users,'name').join(",")
Well you can always override the toString method of your objects:
var arr = [
{name: "Joe", age: 22, toString: function(){return this.name;}},
{name: "Kevin", age: 24, toString: function(){return this.name;}},
{name: "Peter", age: 21, toString: function(){return this.name;}}
];
var result = arr.join(", ");
console.log(result);
You want the concat method.
var finalObj = json1.concat(json2);
Upon first appearance, the word "merg" leads one to think you need to use .extend, which is the proper jQuery way to "merge" JSON objects. However, $.extend(true, {}, json1, json2); will cause all values sharing the same key name to be overridden by the latest supplied in the params. As review of your question shows, this is undesired.
What you seek is a simple javascript function known as .concat. Which would work like:
var finalObj = json1.concat(json2);
While this is not a native jQuery function, you could easily add it to the jQuery library for simple future use as follows:
;(function($) {
if (!$.concat) {
$.extend({
concat: function() {
return Array.prototype.concat.apply([], arguments);
}
});
}
})(jQuery);
And then recall it as desired like:
var finalObj = $.concat(json1, json2);
You can also use it for multiple array objects of this type with a like:
var finalObj = $.concat(json1, json2, json3, json4, json5, ....);
And if you really want it jQuery style and very short and sweet (aka minified)
;(function(a){a.concat||a.extend({concat:function(){return Array.prototype.concat.apply([],arguments);}})})(jQuery);
;(function($){$.concat||$.extend({concat:function(){return Array.prototype.concat.apply([],arguments);}})})(jQuery);
$(function() {
var json1 = [{id:1, name: 'xxx'}],
json2 = [{id:2, name: 'xyz'}],
json3 = [{id:3, name: 'xyy'}],
json4 = [{id:4, name: 'xzy'}],
json5 = [{id:5, name: 'zxy'}];
console.log(Array(10).join('-')+'(json1, json2, json3)'+Array(10).join('-'));
console.log($.concat(json1, json2, json3));
console.log(Array(10).join('-')+'(json1, json2, json3, json4, json5)'+Array(10).join('-'));
console.log($.concat(json1, json2, json3, json4, json5));
console.log(Array(10).join('-')+'(json4, json1, json2, json5)'+Array(10).join('-'));
console.log($.concat(json4, json1, json2, json5));
});
center { padding: 3em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<center>See Console Log</center>
jsFiddle
Try using jQuery.map() - Array.map() not used because of IE < 9 support
For JSON.stringify() - use json2 for old browser support
$.map(array, function(obj){return JSON.stringify(obj)}).join(' ')
Update: To get the value properties
var string = $.map(array, function(obj){
return obj.value
}).join(' ');
Demo: Fiddle
Use Array.map:
let data = [
{
"animal": "cat",
"name": "Fluffy"
},
{
"animal": "dog",
"name": "Bowser"
},
{
"animal": "cat",
"name": "Felix"
}
]
Now extract the names using .map:
let names = data.map(item => item.name)
let nameString = names.join(' ')
And now nameString contains Fluffy Bowser Felix.
Two one-liners:
with lodash:
res = _(json1).concat(json2).groupBy('id').map(_.spread(_.assign)).value();
in ES2015:
res = json2.map(x => Object.assign(x, json1.find(y => y.id == x.id)));
ES2015 georg's answer works great;
json1 = [
{id:1, test: 0},
{id:2, test: 0},
{id:3, test: 0},
{id:4, test: 0},
{id:5, test: 0}
];
json2 = [
{id:1, test: 1},
{id:3, test: 1},
{id:5, test: 1}
];
json1.map(x => Object.assign(x, json2.find(y => y.id == x.id)));
result:
{id:1, test: 1},
{id:2, test: 0},
{id:3, test: 1},
{id:4, test: 0},
{id:5, test: 1}