How to deep clone this part of javascript? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
still very new in js overall and i'm trying to deep clone this part of code.
$scope.add = function() {
$scope.data.push(Object.assign(mock));
};
If anyone could help me cloning this, would be grateful. Thanks

use angular's .copy() method to clone.
like this
$scope.data.push(angular.copy(mock));

If you want to create an actual clone where changes to the clone don't effect the object you are cloning:
Object.assign({}, mock)

If you are using jQuery, another way to deep copy is by using .extend(true, target, object1 [, objectN ] )
https://api.jquery.com/jquery.extend/

Related

Why array is not showing proper result in react? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 19 days ago.
Improve this question
Below is the screen of my VScode, I am declaring arr1 as an array of numbers and later reversing it.Input
The problem is that on output the 1st paragraph is showing the reversed arrays instead of original one. What is the exact problem?
Output
Try this:
let reverse = Array.from(arr1).reverse()
As reverse() method modifies the original array (arr1), then you need to create a new array with Array.from(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
Reverse is a mutating method, so both arr1 and reverse reference the same array. You'll want to do something like const reverse = [...arr1].reverse() instead to ensure that you aren't mutating the original array.

String processing, get last string in vuejs? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
i am dealing with strings in vuejs. Now I have 4 url ​​strings:
https://web-sand.com/product/slug/apple-iphone-13
https://web-sand.com/product/slug/samsung-galaxy
https://web-sand.com/product/slug/xiaomi-red
https://web-sand.com/product/slug/apple-ipad
Now I want to process to get the final string. Since my string is not fixed length using fixed ways is not efficient.
Result I want to get :
apple-iphone-13
samsung-galaxy
xiaomi-red
apple-ipad
Everyone please give me any comments, thanks.
You can use:
function getStr(str) {
return str.split('\/').pop()
}
Here:
input.split("\n").map(line => line.split("/").pop())
[
'https://web-sand.com/product/slug/apple-iphone-13',
'https://web-sand.com/product/slug/samsung-galaxy',
'https://web-sand.com/product/slug/xiaomi-red',
'https://web-sand.com/product/slug/apple-ipad'
].map(item => item.split('/').pop())

How to parse or loop this [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have tried everything I can think of to get the data but cannot figure this out.
The data is in a structure like this:
[{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
I've tried looping, using key:value using the dot notation and bracket notation and cannot get the info.
I'm trying to get to the "languages" so I can parse them for the weather.
var arr = [{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
arr.forEach(function(obj){ //loop array
obj.languages.forEach(function(language) { //loop languages
console.log(language); //language object
console.log(language.lang_name); //language property
});
});

Javascript array of object [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to have an array containing an object. For example I have an object like this:
parentObj:{
childObj1,
childObj2
}
but I want to have a "parentObj" array and be able to call it like :
parentObj[0].childObj1
I searched a lot but didn't find anything related to this. I'll appreciate if someone can help me with this.
thanks very much
Here's an array of objects:
var a = [
{name: "first"},
{name: "second"}
];
console.log(a[0].name); // "first"
console.log(a[1].name); // "second"
The [] is an array initializer. The {} is an object initializer.

Math on values inside an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I was looking for some way to make calculations on values inside array. I know that I can sum the values, sort it or returs max/min. But I am novice and i do not find anything about harder math. I have formula:
http://i.imgur.com/O5tNmMh.jpg
(can't attach images yet...)
And I have an array [somex, somey, somex, somey,somex, somey, ....] Is it possible to calculate the thing like that?
you need some logic like this:
int current=0 ,previous=0,yoursum=0;
for(i=0;i<no. of elements in array;i++)
{
current=arr[i];
else
{
yoursum+=previous-current;
previous=current;
}
}

Categories