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 2 years ago.
Improve this question
I've got example array:
{
"orders": [
{
"order_id": 1,
"email": "alex#alex.com",
"city": "London"
},
{
"order_id": 2,
"email": "tom#tom.com",
"city": "Miami"
}
]
}
and variable var email = "tom#tom.com".
I have to select order_id and city from array where email = "tom#tom.com"
Have you got any ideas how to do it?
By using .filter() and .map():
const data = { "orders": [{ "order_id": 1, "email": "alex#alex.com", "city": "London" }, { "order_id": 2, "email": "tom#tom.com", "city": "Miami" }] };
const result = data.orders.filter(({email}) => email === 'tom#tom.com')
.map(({order_id, city}) => ({ order_id, city }));
console.log(result);
Try this below:
let json = {
"orders": [
{
"order_id": 1,
"email": "alex#alex.com",
"city": "London"
},
{
"order_id": 2,
"email": "tom#tom.com",
"city": "Miami"
}
]
}
var email = "tom#tom.com"
json['orders'].map(function(item){
if (item.email == email){
console.log(item.order_id);
console.log(item.city);
}
});
Related
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 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Using this Object which is a result from the API ( Columns are dynamic )
let json1 ={
"records": [
{
"ID": "1",
"Value": "A005",
},
{
"ID": "2",
"Value": "A007",
},
{
"ID": "3",
"Value": "B001",
},
{
"ID": "4",
"Value": "B003",
},
],
"conditional":[
{"Value":{'A005':'red','A007':'blue','B001':'green'}},
]
}
I need the below Object to format the data to represent in the UI
let json ={
"records": [
{
"ID": "1",
"Value": "<span style='color;red'>A005</span>",
},
{
"ID": "2",
"Value": "<span style='color;blue'>A007</span>",
},
{
"ID": "3",
"Value": "<span style='color;green'>B001</span>",
},
{
"ID": "4",
"Value": "B003",
},
],
"conditional":[
{"Value":{'A005':'red','A007':'blue','B001':'green'}},
]
}
Data inside conditional and records are dynamic. 'Value' can be any column name, it can also have any number of columns. All data are dynamic
We can do it via Array.forEach()
let json ={
"records": [
{
"ID": "1",
"Value": "A005",
},
{
"ID": "2",
"Value": "A007",
},
{
"ID": "3",
"Value": "B001",
},
{
"ID": "4",
"Value": "B003",
},
],
"conditional":[
{"Value":{'A005':'red','A007':'blue','B001':'green'}},
]
}
// PS: conditional format seems not elegant,especially []
let conditional = json.conditional[0].Value
json.records.forEach(e => {
let color = conditional[e.Value]
e.value = `<span style='color:${color}'>` + e.Value + `</span>`
})
console.log(json)
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 5 months ago.
Improve this question
i am having my data from API somethings like:
"data1": {
"items": [
{
"fields": {
"light_voltage": "17.8",
"light_current": "0.40",
"light_power": "7",
"fault_info": "-"
},
"id": 1715
},
{
"fields": {
"light_voltage": "17.1",
"light_current": "0.66",
"light_power": "11",
"fault_info": "-"
},
"id": 1716
},
{
"fields": {
"light_voltage": "17.1",
"light_current": "0.66",
"light_power": "11",
"fault_info": "-"
},
"id": 1717
}
],
"total": 02
}
And Another one is something likes:
"data2": {
"id": 1715,
"latitude": 2.13082,
"longitude": 119.32131,
}
Now i want to compare data1's "id" with data2's "id".
if data1's any of "id" match with data2's "id"'s
then it will displayed rest of values for data2's which are latitude and longitude.(In JavaScript)
Based on your original data, below is a simple reference for you,but I would suggest you to try it with your efforts
const data1 ={"data1": {
"items": [
{
"fields": {
"light_voltage": "17.8",
"light_current": "0.40",
"light_power": "7",
"fault_info": "-"
},
"id": 1715
},
{
"fields": {
"light_voltage": "17.1",
"light_current": "0.66",
"light_power": "11",
"fault_info": "-"
},
"id": 1716
},
{
"fields": {
"light_voltage": "17.1",
"light_current": "0.66",
"light_power": "11",
"fault_info": "-"
},
"id": 1717
}
],
"total":2
}
}
const data2 ={
"data2": {
"id": 1715,
"latitude": 2.13082,
"longitude": 119.32131
}
}
let contains = data1.data1.items.map(i => i.id).some(r => data2.data2.id==r)
if(contains){
console.log(data2.data2.latitude)
console.log(data2.data2.longitude)
}
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 3 years ago.
Improve this question
I have an array of objects which has an object within each element and looks like this
[
{
"person": {
"name": "John",
"isActive": true,
"id": 1
}
},
{
"person": {
"name": "Ted",
"isActive": true,
"id": 2
}
}
]
I would like to convert it into this format which doesn't have the class name:
[
{
"Name": "John",
"IsActive": true,
"id": 1
},
{
"Name": "Ted",
"IsActive": true,
"id": 2
}
]
The result will allow me to parse the information into a Kendo UI grid. The conversion is being done in a Typescript file.
You could map the destructured property person.
var data = [{ person: { name: "John", isActive: true, id: 1 } }, { person: { name: "Ted", isActive: true, id: 2 } }],
result = data.map(({ person }) => person);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Use map and return an object with the required key
let data = [{
"person": {
"name": "John",
"isActive": true,
"id": 1
}
},
{
"person": {
"name": "Ted",
"isActive": true,
"id": 2
}
}
];
let newData = data.map(function(item) {
return {
Name: item.person.name,
IsActive: item.person.isActive,
id: item.person.id
};
});
console.log(newData)
Very Simple
people.map(p=>p.person)
var people=[
{
"person": {
"name": "John",
"isActive": true,
"id": 1
}
},
{
"person": {
"name": "Ted",
"isActive": true,
"id": 2
}
}
]
var newPeople=people.map(p=>p.person)
console.log(newPeople)
You can use .map method
const arr = [{"person": {"name": "John","isActive": true,"id": 1}},{"person": {"name": "Ted","isActive": true,"id": 2}}]
.map(({ person }) => {
return {
"Name": person.name,
"IsActive": person.isActive,
"id": person.id
}
})
console.log(arr)
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 6 years ago.
I have a hundreds of user in the list, below is just an example list :
var userList = [
{
"FullName": "test1",
"UserName": "test1",
"Email": "test1#test.com"
},
{
"FullName": "test2",
"UserName": "test2",
"Email": "test2#test.com"
}
];
var userStr = "test1 is doing a test2";
userList.map((user) => {
userStr.replace(RegExp(user.FullName,"gi"), user.Email)
})
console.log(userStr);
The test string return is still the same and is not replace with email. Is the code I do optimize?
You need to update the variable with the returned value.
var userList = [{
"FullName": "test1",
"UserName": "test1",
"Email": "test1#test.com"
}, {
"FullName": "test2",
"UserName": "test2",
"Email": "test2#test.com"
}];
var userStr = "test1 is doing a test2";
userList.forEach((user) => userStr = userStr.replace(RegExp(user.FullName, "gi"), user.Email))
console.log(userStr);
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 7 years ago.
Improve this question
In JavaScript, I am running a forEch method on an array of objects. Would like to run through my JSON and replace a substring in a string with a new string if the condition is met.
The issue is this: http://www.bucketfeet.comhttps://d153fwbf2sefnf.cloudfront.net/media/artist/d/a/daniel_falsetta_headshot.jpg AND undefined.
Here is the code with JSON data. Thanks for your help in advance. Here is the repl link as well- https://repl.it/BQsZ/19 so you can run the code.
var dataJson = [{
"created_at": "2015-03-27 11:08:00+00:00",
"city": "Pueblo",
"first_name": "daniel",
"last_name": "Falsetta",
"country": "US",
"artist_id": 6709,
"email": "8XC87gZdjX92#example.com",
"profile_image_url": "https://d153fwbf2sefnf.cloudfront.net/media/artist/d/a/daniel_falsetta_headshot.jpg"
},{
"created_at": "2015-03-27 11:08:00+00:00",
"city": "Dallas",
"first_name": "Suah",
"last_name": "Yu",
"country": "US",
"artist_id": 6708,
"email": "BATJP3mht4vd#example.com",
"profile_image_url": ""
},{
"created_at": "2015-03-27 11:08:00+00:00",
"city": "Ottawa ",
"first_name": "Makena",
"last_name": "Ablett",
"country": "CA",
"artist_id": 6710,
"email": "RTFheqUbixmy#example.com",
"profile_image_url": "/media/artist/image-4414.jpg"
},{
"created_at": "2015-03-27 10:53:40+00:00",
"city": "Pittsburgh",
"first_name": "Michelle",
"last_name": "Vecchio",
"country": "US",
"artist_id": 7388,
"email": "kpEFwMqV0Zip#example.com",
"profile_image_url": "http://d153fwbf2sefnf.cloudfront.net/media/artist/i/m/image_473.jpg"
}, {
"created_at": "2015-03-26 18:13:45+00:00",
"city": "",
"first_name": "Wendy",
"last_name": "Slavas",
"country": "",
"artist_id": 28,
"email": "aHrDLA3W5a6p#example.com",
"profile_image_url": ""
}
];
var profUrlFunc = function() {
var objTableArray = dataJson;
objTableArray.forEach(function(user) {
var httpsLinkCloudFront = "https://d153fwbf2sefnf.cloudfront.net"; //length: 37
var httpLinkCloudFront = "http://d153fwbf2sefnf.cloudfront.net"; //length: 36
var prefixLink = "http://www.bucketfeet.com";
var placeHolderForLink = user.profile_image_url;
if(user.profile_image_url) {
//if user profile image has HTTP /cloudfront prefix link remove it and add http://www.bucketfeet.com OR don't add anything
user.profile_image_url = placeHolderForLink.replace("http://d153fwbf2sefnf.cloudfront.net", "www.bucketfeet.com")
}
if (user.profile_image_url) {
//if user profile image has HTTPS /cloudfront prefix link remove it and add http://www.bucketfeet.com OR don't add anything
user.profile_image_url = placeHolderForLink.replace("https://d153fwbf2sefnf.cloudfront.net", "www.bucketfeet.com")
}
if (user.profile_image_url) {
//if no prefix(broken link:/media/artist/image-4414.jpg) add http://www.bucketfeet.com
var newModifiedLinkUrl = prefixLink.concat(placeHolderForLink)
user.profile_image_url = newModifiedLinkUrl;
console.log(user.profile_image_url);
}
else {
//if it is empty give it a default image
user.profile_image_url = "http://www.google.com/favicon.ico";
}
});
};
console.log(profUrlFunc());
Try this
var profUrlFunc = function() {
var objTableArray = dataJson;
objTableArray.forEach(function(user) {
var httpsLinkCloudFront = "https://d153fwbf2sefnf.cloudfront.net";
var httpLinkCloudFront = "http://d153fwbf2sefnf.cloudfront.net";
var prefixLink = "http://www.bucketfeet.com";
var placeHolderForLink = user.profile_image_url;
var condition = new RegExp([httpsLinkCloudFront, httpLinkCloudFront].join('|'));
if (user.profile_image_url) {
if (!condition.test(user.profile_image_url)) {
user.profile_image_url = prefixLink + user.profile_image_url;
} else {
user.profile_image_url = user.profile_image_url.replace(condition, prefixLink);
}
} else {
user.profile_image_url = "http://www.google.com/favicon.ico";
}
});
return objTableArray;
};
Example
You can a bit simplified your function:
first, you have same url for cloud front except protocol part, replace function availabe regex as first parameter.
/https?:\/\/d153fwbf2sefnf.cloudfront.net/
this regex say that s in part https is optional, so this regex match both url with http and with https.
second, you should check that placeHolderForLink already not start from prefixLink before add it.
So you can get something like this:
var profUrlFunc = function () {
var objTableArray = dataJson;
objTableArray.forEach(function (user) {
var LinkCloudFront = /https?:\/\/d153fwbf2sefnf.cloudfront.net/;
var prefixLink = "http://www.bucketfeet.com";
var placeHolderForLink = user.profile_image_url;
if (placeHolderForLink) {
placeHolderForLink = placeHolderForLink.replace(LinkCloudFront, prefixLink);
if (!placeHolderForLink.startsWith(prefixLink)) {
placeHolderForLink = prefixLink.concat(placeHolderForLink);
}
} else {
//if it is empty give it a default image
placeHolderForLink = "http://www.google.com/favicon.ico";
}
user.profile_image_url = placeHolderForLink;
});
};
var dataJson = [{
"created_at": "2015-03-27 11:08:00+00:00",
"city": "Pueblo",
"first_name": "daniel",
"last_name": "Falsetta",
"country": "US",
"artist_id": 6709,
"email": "8XC87gZdjX92#example.com",
"profile_image_url": "https://d153fwbf2sefnf.cloudfront.net/media/artist/d/a/daniel_falsetta_headshot.jpg"
}, {
"created_at": "2015-03-27 11:08:00+00:00",
"city": "Dallas",
"first_name": "Suah",
"last_name": "Yu",
"country": "US",
"artist_id": 6708,
"email": "BATJP3mht4vd#example.com",
"profile_image_url": ""
}, {
"created_at": "2015-03-27 11:08:00+00:00",
"city": "Ottawa ",
"first_name": "Makena",
"last_name": "Ablett",
"country": "CA",
"artist_id": 6710,
"email": "RTFheqUbixmy#example.com",
"profile_image_url": "/media/artist/image-4414.jpg"
}, {
"created_at": "2015-03-27 10:53:40+00:00",
"city": "Pittsburgh",
"first_name": "Michelle",
"last_name": "Vecchio",
"country": "US",
"artist_id": 7388,
"email": "kpEFwMqV0Zip#example.com",
"profile_image_url": "http://d153fwbf2sefnf.cloudfront.net/media/artist/i/m/image_473.jpg"
}, {
"created_at": "2015-03-26 18:13:45+00:00",
"city": "",
"first_name": "Wendy",
"last_name": "Slavas",
"country": "",
"artist_id": 28,
"email": "aHrDLA3W5a6p#example.com",
"profile_image_url": ""
}];
var profUrlFunc = function () {
var objTableArray = dataJson;
objTableArray.forEach(function (user) {
var LinkCloudFront = /https?:\/\/d153fwbf2sefnf.cloudfront.net/;
var prefixLink = "http://www.bucketfeet.com";
var placeHolderForLink = user.profile_image_url;
if (placeHolderForLink) {
placeHolderForLink = placeHolderForLink.replace(LinkCloudFront, prefixLink);
if (!placeHolderForLink.startsWith(prefixLink)) {
placeHolderForLink = prefixLink.concat(placeHolderForLink);
}
} else {
//if it is empty give it a default image
placeHolderForLink = "http://www.google.com/favicon.ico";
}
user.profile_image_url = placeHolderForLink;
});
};
profUrlFunc();
document.getElementById('r').innerHTML = JSON.stringify(dataJson.map(function (el) {
return el.profile_image_url
}), null, 2);
<pre id="r"></pre>
in forEach
if(placeHolderForLink && placeHolderForLink.length) {
user.profile_image_url = (!placeHolderForLink.indexof(httpsLinkCloudFront)) ?
prefixLink+placeHolderForLink.substr(placeHolderForLink.indexOf(httpsLinkCloudFront)+37)
:(!placeHolderForLink.indexof(httpLinkCloudFront)) ?
prefixLink+placeHolderForLink.substr(placeHolderForLink.indexOf(httpLinkCloudFront)+36)
:
prefixLink+placeHolderForLink
}
or, readable and maintainable:
if (placeHolderForLink && placeHolderForLink.length) {
if (!placeHolderForLink.indexof(httpsLinkCloudFront)) {
var1 = prefixLink+placeHolderForLink.substr(placeHolderForLink.indexOf(httpsLinkCloudFront)+37)
} elsif (!placeHolderForLink.indexof(httpLinkCloudFront)) {
var1 = prefixLink+placeHolderForLink.substr(placeHolderForLink.indexOf(httpLinkCloudFront)+36)
} else {
var1 = prefixLink+placeHolderForLink
}
user.profile_image_url = var1
}