How can I get the sites name data value, with entire Formdata array?
Expected output:
["TEST-TRIAL-001120",
"Mubashir Test Site - 001001",
"TEST-TRIAL-001120",
"TEST SITE -001"]
const Formdata = [{
"id": "6efcf3f7-5d29-4a88-8ce9-346229a86765",
"title": "Deepak test form 2",
"version": "1",
"sites": [{
"id": "d7f2b290-2820-401a-a347-9a4dd05ec02b",
"name": "TEST-TRIAL-001120"
}],
"status": "Not Linked"
},
{
"id": "89e5a12d-913d-4f5d-a030-b172af9e27f0",
"title": "Deepak Test form 1",
"version": "1",
"sites": [],
"status": "Not Linked"
},
{
"id": "79a15768-762e-4ff8-b565-31274a38b22d",
"title": "1.7 Form",
"version": "1",
"sites": [{
"id": "32369d1d-f247-4a75-8c14-490c8393dbb8",
"name": "Mubashir Test Site - 001001"
}, {
"id": "d7f2b290-2820-401a-a347-9a4dd05ec02b",
"name": "TEST-TRIAL-001120"
}, {
"id": "a6526163-3ed7-4125-8f32-e0066fc6fe24",
"name": "TEST SITE -001"
}],
"status": "Not Linked"
}
]
Formdata.map(form => {
console.log(form.sites); //not understand how to get sites name data value
});
map over the site array to get an array of names, then flattening that array.
const Formdata=[{id:"6efcf3f7-5d29-4a88-8ce9-346229a86765",title:"Deepak test form 2",version:"1",sites:[{id:"d7f2b290-2820-401a-a347-9a4dd05ec02b",name:"TEST-TRIAL-001120"}],status:"Not Linked"},{id:"89e5a12d-913d-4f5d-a030-b172af9e27f0",title:"Deepak Test form 1",version:"1",sites:[],status:"Not Linked"},{id:"79a15768-762e-4ff8-b565-31274a38b22d",title:"1.7 Form",version:"1",sites:[{id:"32369d1d-f247-4a75-8c14-490c8393dbb8",name:"Mubashir Test Site - 001001"},{id:"d7f2b290-2820-401a-a347-9a4dd05ec02b",name:"TEST-TRIAL-001120"},{id:"a6526163-3ed7-4125-8f32-e0066fc6fe24",name:"TEST SITE -001"}],status:"Not Linked"}];
const result = Formdata.flatMap(({ sites }) => {
return sites.map(({ name }) => name);
});
console.log(result);
Additional documentation
Destructuring assignment
I use the flatMap and map
const Formdata = [{ "id": "6efcf3f7-5d29-4a88-8ce9-346229a86765", "title": "Deepak test form 2", "version": "1", "sites": [{ "id": "d7f2b290-2820-401a-a347-9a4dd05ec02b", "name": "TEST-TRIAL-001120" }], "status": "Not Linked" }, { "id": "89e5a12d-913d-4f5d-a030-b172af9e27f0", "title": "Deepak Test form 1", "version": "1", "sites": [], "status": "Not Linked" }, { "id": "79a15768-762e-4ff8-b565-31274a38b22d", "title": "1.7 Form", "version": "1", "sites": [{ "id": "32369d1d-f247-4a75-8c14-490c8393dbb8", "name": "Mubashir Test Site - 001001" }, { "id": "d7f2b290-2820-401a-a347-9a4dd05ec02b", "name": "TEST-TRIAL-001120" }, { "id": "a6526163-3ed7-4125-8f32-e0066fc6fe24", "name": "TEST SITE -001" }], "status": "Not Linked" } ];
const sites = Formdata.flatMap(({sites}) => sites.map(({name}) => name))
console.log(sites)
First map the sites names from each form. This leaves you with an array of arrays which you need to flatten, which can be done with concat or flatMap
const Formdata = [{"id": "6efcf3f7-5d29-4a88-8ce9-346229a86765","title": "Deepak test form 2","version": "1","sites": [{"id": "d7f2b290-2820-401a-a347-9a4dd05ec02b","name": "TEST-TRIAL-001120"}],"status": "Not Linked"},{"id": "89e5a12d-913d-4f5d-a030-b172af9e27f0","title": "Deepak Test form 1","version": "1","sites": [], "status": "Not Linked"},{"id": "79a15768-762e-4ff8-b565-31274a38b22d","title": "1.7 Form","version": "1","sites": [{"id": "32369d1d-f247-4a75-8c14-490c8393dbb8","name": "Mubashir Test Site - 001001"}, {"id": "d7f2b290-2820-401a-a347-9a4dd05ec02b","name": "TEST-TRIAL-001120"}, {"id": "a6526163-3ed7-4125-8f32-e0066fc6fe24","name": "TEST SITE -001"}],"status": "Not Linked"}]
let sites = [].concat.apply([],
Formdata.map(data =>
data.sites.map(site => site.name)
)
)
/*let sites = Formdata.map(data =>
data.sites.map(site => site.name)
).flatMap(x=>x)*/
console.log(sites)
Related
I have a json file.
[
{
"line": 1,
"elements": [
{
"start_timestamp": "2022-10-17T20:07:41.706Z",
"steps": [
{
"result": {
"duration": 12216552000,
"status": "passed"
},
"line": 5,
"name": "m0e",
"match": {
"location": "seleniumgluecode.book.user_is_on_homepagee()"
},
"keyword": "Given "
},
{
"result": {
"duration": 2074982200,
"status": "passed"
},
"line": 6,
"name": "m1e1",
"match": {
"location": "seleniumgluecode.book.user_enters_Usernamee()"
},
"keyword": "When "
}
],
"tags": [
{
"name": "#Smokee"
}
]
},
{
"start_timestamp": "2022-10-17T20:08:12.284Z",
"steps": [
{
"result": {
"duration": 12090584100,
"status": "passed"
},
"line": 12,
"name": "m0e2",
"match": {
"location": "seleniumgluecode.book2.user_is_on_homepageee()"
},
"keyword": "Given "
}
],
"tags": [
{
"name": "#Smokee"
}
]
}
],
"name": "Login Featuree",
"description": " Verify if user is able to Login in to the sitee",
"id": "login-featuree",
"keyword": "Feature",
"uri": "file:src/test/java/features/tribe/squad1/kitab.feature",
"tags": []
},
{
"line": 1,
"elements": [
{
"start_timestamp": "2022-10-17T20:08:34.480Z",
"steps": [
{
"result": {
"duration": 11366098500,
"status": "passed"
},
"line": 5,
"name": "m0e",
"match": {
"location": "seleniumgluecode.book.user_is_on_homepagee()"
},
"keyword": "Given "
}
],
"tags": [
{
"name": "#Smokee"
}
]
}
],
"name": "Login Featureefghfgh",
"description": " Verify if user is able to Login in to the sitee",
"id": "login-featureefghfgh",
"keyword": "Feature",
"uri": "file:src/test/java/features/tribe1/squad2/kitab2.feature",
"tags": []
},
{
"line": 19,
"elements": [
{
"start_timestamp": "2022-10-17T20:09:40.836Z",
"steps": [
{
"result": {
"duration": 12761711100,
"status": "passed"
},
"line": 23,
"name": "m0e",
"match": {
"location": "seleniumgluecode.book.user_is_on_homepagee()"
},
"keyword": "Given "
}
],
"tags": [
{
"name": "#Smokee"
}
]
}
],
"name": "X Feature",
"description": " Verify if user is able to Login in to the sitee",
"id": "login-featuree",
"keyword": "Feature",
"uri": "file:src/test/java/features/tribe2/test.feature",
"tags": []
}
]
I am getting url addresses in this array
document.addEventListener("DOMContentLoaded", () => {
var i = report.length;
var array = [];
for(x = 0; x < i; x++){
array.push(report[x].uri.split("/"));
}
console.log(array2);
});
This return me :
0:
(7) ['file:src', 'test', 'java', 'features', 'tribe1', 'squad1', 'kitab.feature']
1:
(7) ['file:src', 'test', 'java', 'features', 'tribe1', 'squad2', 'kitab2.feature']
2:
(6) ['file:src', 'test', 'java', 'features', 'tribe2, kitab3.feature']
I don't need file:src, test, java, features. Deleting them in 3 arrays and getting a unique array like this:
0:
(3) ['tribe1', 'squad1', 'kitab.feature']
1:
(3) ['tribe1', 'squad2', 'kitab2.feature']
2:
(2) ['tribe2, kitab3.feature']
Finally, if there are 2 elements before the .feature, I need to create a new array by considering 1 as squad and 2 as tribe. Like this:
Diagram
[tribe1
squad1
elem
1
2
name
url
squad2
elem
1
2
name
url
tribe2
elem
1
2
name
url
]
How can I do that?. Thank you.
You should try destructing the array. An example is shown below:
[a,b,c,d, ...rest] = ['file:src', 'test', 'java', 'features', 'tribe1', 'squad1', 'kitab.feature']
console.log(rest);
You can transform or create a new array based on input array with this simple logic with the help of Array.map() along with String.split() and Array.splice() method.
Live Demo :
const arr = [
{
"line": 1,
"uri": "file:src/test/java/features/tribe/squad1/kitab.feature"
},
{
"line": 1,
"uri": "file:src/test/java/features/tribe1/squad2/kitab2.feature"
},
{
"line": 19,
"uri": "file:src/test/java/features/tribe2/test.feature"
}
];
const res = arr.map(({ uri}) => uri.split('/').splice(4));
console.log(res);
This question already has answers here:
JavaScript: How to join / combine two arrays to concatenate into one array?
(3 answers)
Closed 2 years ago.
var array= [
{
"Name": "mohan",
"DESCRIPTION": "Vendor"
},
{
"Name": "mahesh",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manoj",
"DESCRIPTION": "Vendor2"
}]
var array2= [
{
"Name": "mohanraj",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manojkumar",
"DESCRIPTION": "Vendor2"
},
{
"Name": "mohankumar",
"DESCRIPTION": "Vendor3"
}];
expected output
array3=[
{
"Name": "mohan",
"DESCRIPTION": "Vendor"
},
{
"Name": "mahesh",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manoj",
"DESCRIPTION": "Vendor2"
},
{
"Name": "mohanraj",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manojkumar",
"DESCRIPTION": "Vendor2"
},
{
"Name": "mohankumar",
"DESCRIPTION": "Vendor3"
}
];
You can do it like this
const arraymain = [
{
"Name": "mohan",
"DESCRIPTION": "Vendor"
},
{
"Name": "mahesh",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manoj",
"DESCRIPTION": "Vendor2"
}]
const arraysecond = [
{
"Name": "mohanraj",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manojkumar",
"DESCRIPTION": "Vendor2"
},
{
"Name": "mohankumar",
"DESCRIPTION": "Vendor3"
}];
const arraythird = array3=[
{
"Name": "mohan",
"DESCRIPTION": "Vendor"
},
{
"Name": "mahesh",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manoj",
"DESCRIPTION": "Vendor2"
},
{
"Name": "mohanraj",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manojkumar",
"DESCRIPTION": "Vendor2"
},
{
"Name": "mohankumar",
"DESCRIPTION": "Vendor3"
}
];
const arrayfinal = arraymain.concat(arraysecond,arraythird);
console.log(arrayfinal);
const array3 = array1.concat(array2):
See Array.prototype.concat
const array3 = [...array1, ...array2]
this will do shallow copy, which will avoid reference issues upto a level.
You can Array.prototype.concat method which allows to append value of one array to another Array
let first_array = [1,2,3,4];
let second_array = [5,6,7,8];
let results = first_array.concat(second_array);
console.log(results);
In you case you can perform that this way
let array3 = array.concat(array2):
I think that it very important to take your time learning the basics of Javascript.There ar 1000 ways to achieve this.
var array= [
{
"Name": "mohan",
"DESCRIPTION": "Vendor"
},
{
"Name": "mahesh",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manoj",
"DESCRIPTION": "Vendor2"
}]
var array2= [
{
"Name": "mohanraj",
"DESCRIPTION": "Vendor1"
},
{
"Name": "manojkumar",
"DESCRIPTION": "Vendor2"
},
{
"Name": "mohankumar",
"DESCRIPTION": "Vendor3"
}];
//Without ES6
var arr3=array.concat(array2)
//With ES6 syntax
cons arr3=[...arr,...arr2]
For the rest learn the basics of Javascript...
The Array() constructor is used to create Array objects. You can create the third array by passing the elements of the first and second array elements as arguments. You could do that by using the spread operator.
let newArray = new Array(...firstArray, ...secondArray)
I'm trying to pull out the individual images in the "images" object below under "details" section.
Seem to just be getting nothing printing out. Looking for the correct way to pull within the details.images.image1,2, or 3.
Here is the JSON data I'm working with so far:
{
"books": [
{
"title": "title 1",
"image": "/image1.jpg"
},
{
"title": "title 2",
"image": "/image2.jpg"
}
],
"details": [
{
"author": "book author",
"name": "Book name",
"price": 34.99,
"publisher": "Penguin Books",
"images": [
{
"image1": "/image1.jpg",
"image2": "/image2.jpg",
"image3": "/image3.jpg"
}
]
}
]
}
Also here is the JSON call I'm making in a Book component:
{staticdata.details.map(detail => (
<Book
book_name={detail.author}
book_price={detail.price}
image={detail.images.image1}
/>
))}
Here's an example of accessing those nested properties and logging them to the console. It appears your attempt was mostly correct, but images is an array.
const data = {
"books": [
{
"title": "title 1",
"image": "/image1.jpg"
},
{
"title": "title 2",
"image": "/image2.jpg"
}
],
"details": [
{
"author": "book author",
"name": "Book name",
"price": 34.99,
"publisher": "Penguin Books",
"images": [
{
"image1": "/image1.jpg",
"image2": "/image2.jpg",
"image3": "/image3.jpg"
}
]
}
]
}
data.details.map(detail => {
console.log(detail.author, detail.price, detail.images[0].image1);
});
I have a nested JSON returned from an API that I am hitting using a GET request, in POSTMAN chrome app. My JSON looks like this
"result": [
{
"_id": "some_id",
"name": "India",
"code": "IN",
"link": "http://www.india.info/",
"closingTime": "2017-02-25T01:12:17.860Z",
"openingTime": "2017-02-25T06:12:17.205Z",
"image": "image_link",
"status": "online",
"serverStatus": "online",
"games": [
{
"_id": "some_game_id1",
"name": "Cricket"
},
{
"_id": "some_another_id1",
"name": "Baseball"
},
{
"_id": "some_another_id_2",
"name": "Basketball"
}
]
},
{
"_id": "some_id",
"name": "Australia",
"code": "AUS",
"link": "https://www.lonelyplanet.com/aus/adelaide",
"closingTime": "2017-02-28T05:13:38.022Z",
"openingTime": "2017-02-28T05:13:38.682Z",
"image": "some_image_url",
"status": "offline",
"serverStatus": "online",
"games": [
{
"_id": "some_game_id_2",
"name": "Cricket"
},
{
"_id": "some_another_id_3",
"name": "Kho-Kho"
},
{
"_id": "some_another_id_4",
"name": "Badminton"
},
{
"_id": "some_another_id_5",
"name": "Tennis"
}
]
},
I am trying to test whether my response body has "name":"India" and the "game" with "some_game_id1" contains the "name":"cricket".
I went through this link where the answer is to have an array for "name"created and then check within the array whether the array contains the value. I tried this but my code fails.
Also, I tried searching the element by the index within the JSON body using this -
var searchJSON = JSON.parse(responseBody);
tests["name contains India"] = searchJSON.result.name[0]==="India";
But this also fails. I tried using the .value appended with the second line of above code, but it also fails. How can I check this thing?
You need to put [0] after result (which is an array) rather than name (which is a string).
Also, use a regular expression to check whether the name contains 'India', because using === only checks if the name is exactly India.
var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)
Demo Snippet:
var responseBody = `{
"result": [{
"_id": "some_id",
"name": "India",
"code": "IN",
"link": "http://www.india.info/",
"closingTime": "2017-02-25T01:12:17.860Z",
"openingTime": "2017-02-25T06:12:17.205Z",
"image": "image_link",
"status": "online",
"serverStatus": "online",
"games": [{
"_id": "some_game_id1",
"name": "Cricket"
},
{
"_id": "some_another_id1",
"name": "Baseball"
},
{
"_id": "some_another_id_2",
"name": "Basketball"
}
]
},
{
"_id": "some_id",
"name": "Australia",
"code": "AUS",
"link": "https://www.lonelyplanet.com/aus/adelaide",
"closingTime": "2017-02-28T05:13:38.022Z",
"openingTime": "2017-02-28T05:13:38.682Z",
"image": "some_image_url",
"status": "offline",
"serverStatus": "online",
"games": [{
"_id": "some_game_id_2",
"name": "Cricket"
},
{
"_id": "some_another_id_3",
"name": "Kho-Kho"
},
{
"_id": "some_another_id_4",
"name": "Badminton"
},
{
"_id": "some_another_id_5",
"name": "Tennis"
}
]
}
]
}`
var tests = {}
var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)
console.log(tests) //=> { "name contains India": true }
I have a model in which values are stored in following format:--
Language-count=3
[0]
-ID="1"
-Name="French"
[1]
-ID="2"
-Name="English"
[2]
-ID="3"
-Name="Hindi"
Titles-count=2
[0]
-ID="1"
-Name="Video1"
[1]
-ID="2"
-Name="Video2"
Countries-count=2
[0]
-ID="1"
-Name="India"
[1]
-ID="2"
-Name="USA"
and I have to convert this model in given json format:-
var models = [
{
name: 'Language',
values: [
'English',
'French',
'Hindi'
]
},
{
name: 'Title',
values: [
'Title 1',
'Title 2'
]
},
{
name: 'Countries',
values: [
'India',
'UK'
]
}
];
In above json format I have hard coded those values of Languages,countries and Titles but I have to fetch it from the above model which I have already given.
The json Format which I am getting is following:--
{
"ID": 1,
"DealID": 1,
"Title": "Position1",
"Titles": [
{
"Icon": "hdtv",
"Name": "\nWedding Bells & Farewells\n",
"ID": 12
},
{
"Icon": "hdtv",
"Name": "Delta Farce",
"ID": 5
},
{
"Icon": "hdtv",
"Name": "Doe B: Let Me Find",
"ID": 9
}
],
"Episodes": [
{
"Icon": "episode",
"Name": "Sparkle",
"ID": 4
},
{
"Icon": "episode",
"Name": "Sparks Fly Out",
"ID": 2
},
{
"Icon": "episode",
"Name": "Uploads by Filmi Gaane",
"ID": 7
}
],
"Assets": [
{
"Icon": "file-o",
"Name": "Best of Javed Akhtar - Jukebox 2 - Javed Akhtar Top 10 Hit Songs",
"ID": 10
},
{
"Icon": "file-o",
"Name": "Ep 105 - Sin Say Shun Awards After Party additional image 1",
"ID": 4
},
{
"Icon": "file-o",
"Name": "Ep 105 - Sin Say Shun Awards After Party box cover",
"ID": 3
}
],
"Documents": [],
"Languages": [
{
"Icon": "globe",
"Name": "Albanian",
"ID": 70
},
{
"Icon": "globe",
"Name": "Amharic",
"ID": 96
}
],
"Territories": [],
"Countries": [
{
"Icon": "globe",
"Name": "Afghanistan",
"ID": 2
},
{
"Icon": "globe",
"Name": "Albania",
"ID": 3
},
{
"Icon": "globe",
"Name": "Algeria",
"ID": 4
}
],
"Rights": [
{
"Icon": "leaf",
"Name": "Ancillary",
"ID": 23
},
{
"Icon": "leaf",
"Name": "Finshed Episode Rights",
"ID": 20
},
{
"Icon": "leaf",
"Name": "Format Group - DO NOT USE",
"ID": 63
}
],
"Contributors": [],
"Transmissions": [],
"Available": null
}
It would be best to write a simple parser and transform your data type to JSON - which would additionally allow you to reuse the parser in the future, and convert it to other data types easily for instance.
You could look at the various YAML parsers for inspiration, which would use a similiar technique for your data set's language.
Alternatively you can create a 'hack' and just keep splitting things up if your data format is always of this format, and doesn't allow arbitrary value nesting.
List personel = new List();
var client = new RestClient("your adres");
var request = new RestRequest(Method.GET);
request.AddHeader("Postman-Token", "2893de4a-457e-46a7e8efb025");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("token", "a23a80f7-3323-4594056");
IRestResponse response = client.Execute(request);
JObject deger = JObject.Parse(response.Content);
var toplam = deger["data"]["data"].Count();
string jenp = toplam.ToString();
for (int i = 0; i < toplam; i++)
{
Personeller data = new Personeller();
data.Adi = deger["data"]["data"][i]["adi"].ToString();
data.Soyadi = deger["data"]["data"][i]["soyadi"].ToString();
data.tckimlikno = (long)deger["data"]["data"][i]["tckimlikno"];
personel.Add(data);
}