I have two associative arrays and want to push one completely to the other.
my current code:
LT_WORK_PACKAGE.data[i].STOCK_DOCCAT = stock_data.STOCK_DOCCAT;
LT_WORK_PACKAGE.data[i].STOCK_DOCNO = stock_data.STOCK_DOCNO;
LT_WORK_PACKAGE.data[i].STOCK_ITMNO = stock_data.STOCK_ITMNO;
im looking for something like this:
LT_WORK_PACKAGE.data[i].push(stock_data);
.push is for adding items to an array. You do have an object , and to copy multiple properties into an object,you can use Object.assign:
Object.assign(
/*to:*/ LT_WORK_PACKAGE.data[i],
/*from:*/ stock_data
);
You can use LT_WORK_PACKAGE.data[i] = stock_data.
Note that the previous content (if it exists) of LT_WORK_PACKAGE.data[i] will be replaced by a reference to stock_data. Any changes made in stock_data will be done in LT_WORK_PACKAGE.data[i] If you want a copy, you can use : LT_WORK_PACKAGE.data[i] = JSON.parse(JSON.serialize(stock_data))
Related
does anyone know how I can remove a surrounding array from an array of objects? In my case, I only have one object in this array. [{"id":"6","email":"test#test.com"}] Also, would the solution work in the case of multiple objects in the array? Thanks!
You have an array of objects. That's great, because it's the easiest way to store and manipulate lists of records.
You can use array methods like .map that allow you to treat each record separately, telling it what to do individually with each element. That's also great, because it basically "removes the element from the array" while still processing the whole array, which is what I think you're after.
Simple example to create a dropdown:
const data = [{"id":"6","email":"test#test.com"}, {"id":"12","email":"test2#test.com"}];
const drawEmailDropdown = () => {
let options = data.map(d => `<option value='${d.id}'>${d.email}</option>`);
return `<select>` + options.join("") + `</select>`;
};
document.querySelector("#container").innerHTML = drawEmailDropdown();
<div id="container"></div>
So very quick question here which I wasn't able to get sorted when searching google.
I have some code that works which has a Map object this.tweet and a (key,value) of (string,array). I push a value into the array and re-set Map object.
const newTweet = this.tweet.get(tweetName) || [];
newTweet.push(time);
this.tweet.set(tweetName, newTweet);
However, I am a minimalist freak and want a one-liner. When I want to add something to the array, I was wondering why I am not able to do this
this.tweet.set(tweetName, newTweet.push(time));
I keep getting a newTweet.push(time) is not a function error.
Thanks
Look at some documentation for push
The push() method adds one or more elements to the end of an array and returns the new length of the array.
Since you want to pass the array to set you can't use the return value of push.
You could create a completely new array instead:
const newTweet = this.tweet.get(tweetName) || [];
this.tweet.set(tweetName, [...newTweet, time]);
I'm trying to merge multiple arrays from one object using JavaScript(React-Native), the example of the object is :
{"10419": ["37046", "37047"], "9138": ["32809"]}
The result should be like so:
["37046","37047","32809"]
I need to ignore the object name and end up with only one flat array
I used flat() function but it seems not working as I need.
my try looks like :
var obj = this.state.obj // will contain the object
console.log(obj.flat()) // I know that work only with arrays but I tried it out
Thanks
Using Object.values, you can get the values inside the object. That will be 2d array from your input.
Using Array.prototype.flat, you can make it as 1d array as follows.
const input = {"10419": ["37046", "37047"], "9138": ["32809"]}
const result = Object.values(input).flat();
console.log(result);
Object.values(this.state.obj).flat()
I want to clone my array of objects. Every one tells to use slice(0) but i don't understand, it's not working, check the fiddle. The property is avalaible in the two arrays.
I want to edit my objects in the first array and not in my second too.
I already checked : How to clone a Javascript Array of Objects? It's the same with concat, extend. The only one i found is JSON.parse. I want to understand why people say the method slice cloned arrays.
var arr = [{'obj1':1}, {'obj2':2}];
var clone = arr.slice(0);
clone[0].test = "defined";
https://jsfiddle.net/zkzv2mp0/2/
The fiddler is pure JS. does not havejquery included.
See working example:
https://jsfiddle.net/zkzv2mp0/3/
But as you can see using slice(0); is a Shallow clone, where you want to update one without updating the original (deep clone) JSON.parse(JSON.stringify(arr))
var clone = JSON.parse(JSON.stringify(arr));
You could use JSON Stringify and JSON Parse to clone an Array. But be careful this only works if your array contains JSON serializable content.
Ain't cristal clear what you want to do...
But here is what I think you try to achieve:
// Your initial array of objects.
var arr = [{'obj1':1}, {'obj2':2}];
// A cloned array of objects.
var clone = jQuery.extend(true, [], arr);
// Now sliced, keep the first object only
clone = clone.slice(0,1);
console.log(clone);
// Change the value of the object.
clone[0].obj1 = "defined";
console.log(clone);
// Show them in page.
$("#testarr").html(arr[0].obj1);
$("#testclone").html(clone[0].obj1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
In arr :
<span id="testarr">
</span></p>
<p>
In clone :
<span id="testclone">
</span>
</p>
I have an Json array like data {"alphaNumeric":[]}.
Here I just want to push the another array [mentioned below] of objects to this Data with out loop concept.
data{"numeric":[{"id":"1","alpha":"a"},{"id":"2","alpha":"b"}]}.
I used the below code :
data.alphaNumeric.push(data.numeric);
but the output is :
data{"alphaNumeric":[[{"id":"1","alpha":"a"},{"id":"2","alpha":"b"}]]}.
Expected :
data{"alphaNumeric":[{"id":"1","alpha":"a"},{"id":"2","alpha":"b"}]}.
Help me to resolve.
One solution may be using the concat method. Which isn't really good as it creates a whole new array.
b.alphaNumeric = b.alphaNumeric.concat(a.numeric);
But there is a much better solution using push. It accepts more than just one element, but unfortunately not as an array. This can be however achieved using its apply method:
b.alphaNumeric.push.apply(b.alphaNumeric, a.numeric);
Also you can write your own method (I call it add) which will do this action for you:
Array.prototype.add = function (array) {
this.push.apply(this, array);
return this;
};
b.alphaNumeric.add(a.numeric);
Use concat()
data.alphaNumeric.concat(data.numeric);
.push() and .pop() are for adding and removing single elements in an array. The return value from .concat() is what you're looking for:
var newArr = oldArr.concat(extraArr);