ES6 clon array that is inside an object - ecmascript-6

I have an object state with a key shoppingCart, that contains an array. I know ES6 way to clone an array is let newArray = [...originalArray], but I can't find a way to clone the array in state.shoppingCart. I tried let newShoppingCart = [...state.shoppingCart], but I get a syntax error.

Related

lua dkjson get value from json array

local json = require ("dkjson")
local obj = {"dogs":[{"photo":"http://example.com/1.jpg","price":20}]}
obj_t = json.decode(obj)
I'm use construction: obj_t.dogs.photo and this return nil
This is not a valid Lua code, but if we fix the string (local obj = [==[{"dogs":[{"photo":"http://example.com/1.jpg","price":20}]}]==]) then it looks like the problem is that you have a map with an array that has a map in it, so you're missing accessing the array element. You need to use something like obj_t.dogs[1].photo.

How to get a data from JSON in React Native?

When I run the below code in my react-native project
console.log("Response= "+JSON.stringify(response));
I can get output like below in my console.
Response= {"assets":[{"height":3888,"uri":"file:///data/user/0/com.facebook2/cache/rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","width":5184,"fileName":"rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","type":"image/jpeg","fileSize":1914937}]}
How I print the 'uri' from that JSON response ?
Looking at your data we can break it down by what we see. So, with JSON we have a Javascript Object which contains a param of assets. So to print assets we would console.log(response.assets)
Assets is an array with one item, so we want to get the first item from that which would be console.log(response.assets[0]).
Then we want the uri from that first assets object which would be console.log(response.assets[0].uri)
Hope this is what you are looking for.
You can use Dot Notation to access the properties of an object.
In your response json, it is seen that it has an array with name assets. The required property uri is inside the array. You can access it simply by
response.assets[0].uri
if there were multiple items in your assets array, you can simply loop over the array and get the values,
const length = response.assets.length;
for(let i=0; i< length; i++)
console.log('URI is = ', response.assets[i].uri)
const response = {"assets":[{"height":3888,"uri":"file:///data/user/0/com.facebook2/cache/rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","width":5184,"fileName":"rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","type":"image/jpeg","fileSize":1914937}]};
console.log('URI =', response.assets[0].uri)

Angular 4 removing duplicates from an Array Object

I am having json array object in my Angular 4 application and where i need to find out unique space type in my JSON array.
Json Array:
[{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Hospital_Lab"}},
{"label":{"Space_Type":"Office_OpenOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},{},
{"label":{"Space_Type":"Office_PrivateOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Hospital_Lab"}},{"label":{"Space_Type":"Office_OpenOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_OpenOffice"}},{}]
I want to remove all the duplicate space type .Can anyone tell me how i can get.
I want the result :
[{Idx:0,Label:"Office_PrivateOffice"},{Idx:1,Label:"Office_OpenOffice"}]
You can use lodash method _.uniqWith
var jsonarray = [{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Hospital_Lab"}},
{"label":{"Space_Type":"Office_OpenOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},{},
{"label":{"Space_Type":"Office_PrivateOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Hospital_Lab"}},{"label":{"Space_Type":"Office_OpenOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_OpenOffice"}},{}];
var filtered = _.uniqWith(jsonarray, _.isEqual);
console.log(filtered);
<script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>
The lodash method _.uniqBy(root, 'duplicateElement'); can also be used. The advantage of using this method is that you can tell lodash which element you would like to remove duplicate of.
var newJsonFile = _.uniqBy(label, 'Space_Type');
console.log(newJsonFile);
In Angular, you would need to download package lodash
npm install lodash
Then import it
import * as _ from 'lodash';

parsing and applying conditional element on key value pair in typescript

I am doing an Ionic App with typescript.
I have some error condition as response from REST API,
I did
err._body
and it gives me
{"reason":"invalid_token"}
but when I do
err._body.reason
or
err._body.get("reason")
it gives undefined value.
I did JSON stringify and parse as well, no luck,
How to parse this and get the value so that I can apply specific processing for this.
First try to do
console.log(typeof err._body);
so we can be sure what the type of that is. If it's a string, you should do
let errorObj = JSON.parse(err._body);
// And then...
let errorMsg = errorObj.reason // or errorObj["reason"] as well
If it's an Object, you can skip the parse() part and just use it like this:
let errorMsg = err._body.reason // or err._body["reason"]

trying to convert data from Domino Access Service to a JSON string via JSON.stringify

I want to store the result from a call to a Domino Access Service (DAS) in a localStorage however when I try to convert the result object to a JSON string I get an error.
With DAS you get the result as an Array e.g.:
[
{
"#entryid":"1-CD90722966A36D758025725800726168",
"#noteid":"16B46",
Does anyone know how I get rid of the square brackets or convert the Array quickly to a JSON object?
Here is a snippet of my code:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
localStorage.setItem('myCatalog',JSON.stringify(data));
}
});
Brackets are part of the JSON syntax. They indicate that this is an array of objects. And as you point to a view it is very likely that you would get more than one object back (one for each entry in the view).
So if you are only interested in the first element you could do this:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
var firstRecord = data[0] || {};
localStorage.setItem('myCatalog',JSON.stringify(firstRecord));
}
});
Otherwise, you would need to define a loop to handle each of the objects :-)
/John