Match text to nested array and return Id value - angular

I am trying to match text to values in a nested array. However the result that I need from the array is the roles ID. eg. I have text called HR Admin, now I want to search the object under the "roles" object and return the ID which is "4"
Here is my code
Array
{
"genders": null,
"races": null,
"titles": null,
"maritalStatus": null,
"nationality": null,
"identityTypes": null,
"roles": {
"1": "System Admin",
"2": "Internal Account Manager",
"3": "CAT Manager",
"4": "HR Admin",
"5": "HR Manager",
"6": "HR Recruiter",
"7": "Candidate",
"8": "Operations administrator"
},
"companyTypes": null,
"industries": null,
"fileTypes": null
}
I am trying to filter but am not getting anywhere
const myFilter = (resp, num) => {
const values = resp.filter(det => !!det.roles.find(l => l === num));
return values.map(res => {
res.roles = res.roles.filter(l => l === num);
return res;
});
};

Just find the index of the given value ("HR Admin") and get the key for that same index:
const obj = {
"genders": null,
"races": null,
"titles": null,
"maritalStatus": null,
"nationality": null,
"identityTypes": null,
"roles": {
"1": "System Admin",
"2": "Internal Account Manager",
"3": "CAT Manager",
"4": "HR Admin",
"5": "HR Manager",
"6": "HR Recruiter",
"7": "Candidate",
"8": "Operations administrator"
},
"companyTypes": null,
"industries": null,
"fileTypes": null
};
const search = 'HR Admin';
const index = Object.values(obj.roles).indexOf(search);
const result = Object.keys(obj.roles)[index];
console.log(result);

Related

react-table how create column headers and cells with array

i'm using react-table in my project and i wonder how create headers dynamically with array.
in my component i have:
const columns = [
{Header: 'day', accessor: ''},
{Header: 'day', accessor: ''}
]
<ReactTable
className="-striped -highlight"
data={this.props.items}
columns={columns}
defaultPageSize={20}/>
and i have json data like that:
"items": [
{
"day": "01",
"city": "A",
"1": "",
"2": "",
"3": "",
"4": null,
"5": "",
"6": 256,
"7": 36.07,
"8": 35.15,
"9": "",
"10": "",
"11": 6.49,
"12": 5.9,
"13": "",
"14": "",
"15": 72.0,
"16": 62.109375,
"17": 266.78,
"18": 83.59375,
"19": 444.96
},
{
"day": "02",
"city": "B",
"1": "",
"2": "",
"3": "",
"4": null,
"5": "",
"6": 234,
"7": 36.52,
"8": 35.6,
"9": "",
"10": "",
"11": 6.08,
"12": 5.71,
"13": "",
"14": "",
"15": 64.0,
"16": 43.162393162393165,
"17": 121.97,
"18": 84.1880341880342,
"19": 346.49
},
{
"day": "03",
"city": "B",
"1": "",
"2": "",
"3": "",
"4": null,
"5": "",
"6": 221,
"7": 36.96,
"8": 35.93,
"9": "",
"10": "",
"11": 5.82,
"12": 5.28,
"13": "",
"14": "",
"15": 56.99999999999999,
"16": 39.366515837104075,
"17": 94.48,
"18": 78.28054298642535,
"19": 227.4
},
]
so here is i want to make each item in items array will have column. and these columns headers will be their day property and cells will be values which in list 1, 2, 3, 5, 6. How can i do that ?
You could just have a loop in a function to dynamically create the headers you need based on the first object of your items list :
getColumns () {
let columns = [];
let headers = Object.keys(items[0]);
headers.forEach(header => {
columns.push({
Header: header
accessor: ""
})
}
return columns;
}
Hope it is what you were looking for (unless I didn't understand the question).
UPDATE
The first thing to do is to build the rows array to fit the way you want to display them. Indeed, the items array should be transformed into something like this :
[{
"day01": "A",
"day02": "B",
"day03": "B"
},
{...}]
It can be done like this :
render () {
const rows = this.buildDataFromItems(items); // items is your json array
const columns = this.buildColumnsFromItems(items);
return(
<ReactTable
columns={columns}
data={rows}
/>
)
}
buildColumnsFromItems (items) {
let headers = [];
items.forEach(item => {
headers.push("day" + item.day);
})
let columns = [];
headers.forEach(header => {
columns.push({
Header: header,
accessor: header
})
});
return columns;
}
buildDataFromItems (items) {
let rows = [];
let currentHeader = "";
let currentRow = {};
for (let i = 0; i < Object.keys(items[0]).length && Object.keys(items[0])[i] !== "day"; i++) {
currentRow = {};
items.forEach(item => {
currentHeader = "day" + item.day;
currentRow[currentHeader] = item[Object.keys(items[0])[i]];
})
rows.push(currentRow);
}
return rows;
}
This piece of code should be enough to lead you to your final solution I hope :)

Compare and map the object using angular 4

I have 2 objects, in this mydata object i have to countrycode to country name.
country name i have to pick it from countries object.
countries = [
{
"name": "Afghanistan",
"code": "AF",
"localizationLang": "en"
},
{
"name": "Albania",
"code": "AL",
"localizationLang": "en"
}
];
mydata = [{
"orgCode": "ZC27693542",
"orgName": "B - Sedex Conference Demo Purpose Only",
"siteCode": null,
"siteName": null,
"countryCode": "AF",
"state": null,
"product": [],
"productArea": [],
"lastDateAudit": null,
"saqPercentage": 0.0,
"auditCode": null
},...
];
And i want the output object should be
[{
"orgCode": "ZC27693542",
"orgName": "B - Sedex Conference Demo Purpose Only",
"siteCode": null,
"siteName": null,
"countryCode": "Afghanistan",
"state": null,
"product": [],
"productArea": [],
"lastDateAudit": null,
"saqPercentage": 0.0,
"auditCode": null
},...
]
I need to change the country code to country name.
Easy :)
mydata.map(x=>{
x.countryCode = countries.find(c=>c.code === x.countryCode).name;
return x;
})
try it
var countries = [{
"name": "Afghanistan",
"code": "AF",
"localizationLang": "en"
}, {
"name": "Albania",
"code": "AL",
"localizationLang": "en"
}];
var mydata = [{
"orgCode": "ZC27693542",
"orgName": "B - Sedex Conference Demo Purpose Only",
"siteCode": null,
"siteName": null,
"countryCode": "AF",
"state": null,
"product": [],
"productArea": [],
"lastDateAudit": null,
"saqPercentage": 0.0,
"auditCode": null
}, {
"orgCode": "ZC27695039",
"orgName": "Sonata-AB-Member",
"siteCode": "ZS27695161",
"siteName": "UEM group",
"countryCode": "AL",
"state": null,
"product": [],
"productArea": ["50203200", "50201700", "50202300"],
"lastDateAudit": null,
"saqPercentage": 13.0,
"auditCode": null
}];
var newArr = mydata.map(function(value) {
var country = countries.find(function(countrie) {
return countrie.code == value.countryCode;
});
value.countryCode = country.name;
return value;
});
console.log(newArr)
demo:https://codepen.io/anon/pen/QmeEXe

How to parse a Json object for the desired objects

I am looking to parse a json url/file and get only the top nodes from the json.
The json object looks like this:
{
"2017080350": {
"home": {
"score": {
"1": null,
"2": null,
"3": null,
"4": null,
"5": null,
"T": null
},
"abbr": "ARI",
"to": null
},
"away": {
"score": {
"1": null,
"2": null,
"3": null,
"4": null,
"5": null,
"T": null
},
"abbr": "DAL",
"to": null
},
"bp": 0,
"down": null,
"togo": null,
"clock": null,
"posteam": null,
"note": null,
"redzone": null,
"stadium": "Tom Benson Hall of Fame Stadium",
"media": {
"radio": {
"home": null,
"away": null
},
"tv": "NBC",
"sat": null,
"sathd": null
},
"yl": null,
"qtr": null
},
"2017080351": {
"home": {
"score": {
"1": null,
"2": null,
"3": null,
"4": null,
"5": null,
"T": null
},
"abbr": "ARI",
"to": null
},
"away": {
"score": {
"1": null,
"2": null,
"3": null,
"4": null,
"5": null,
"T": null
},
"abbr": "DAL",
"to": null
},
"bp": 0,
"down": null,
"togo": null,
"clock": null,
"posteam": null,
"note": null,
"redzone": null,
"stadium": "Tom Benson Hall of Fame Stadium",
"media": {
"radio": {
"home": null,
"away": null
},
"tv": "NBC",
"sat": null,
"sathd": null
},
"yl": null,
"qtr": null
}
}
What I'm trying to get are the two values 2017080350 and 2017080351
I can read and display the contents fine but I am having a issue when attempting to get the two mentioned nodes?
If your response is not yet parsed, then first do:
response = JSON.parse(response);
And then you could use a for...in loop:
for (let key in response) {
console.log(key);
}
To read the data linked to the key, you would expand like this:
for (let key in response) {
console.log(key);
let value = response[key];
// and you can go deeper...
for (let deeperKey in value) {
// ...etc.
}
}

Return json from two classes in model

I'm trying to return json with two classes in my model
public function both()
{
return $this->belongsToMany(Car::class)->and(Driver::class)->withPivot('type'); // this is wrong
}
the reason I'm trying to get this like that is because in my function
public function check()
{
$user = JWTAuth::parseToken()->authenticate();
$id = $user->id;
$result = User::with('both')->where('id', $id)->get();
return response()->json($result);
}
right now in my model I have this
public function both()
{
return $this->belongsToMany(Car::class)->withPivot('type');
}
public function driver()
{
return $this->belongsToMany(Driver::class)->withPivot('type');
}
But the function returns json that has a different structure when both ends.
My question is can I have the function return a json with the same structure?
like this
[
{
"id": 4,
"name": null,
"phone": "9000",
"size_of_house": null,
"created_at": "2016-12-04 13:55:52",
"updated_at": "2017-03-08 14:03:44",
"deleted_at": null,
"both": [
{
"id": 177,
"name": "NIS",
"created_at": "2016-12-27 10:28:29",
"updated_at": "2016-12-27 10:28:29",
"pic": "http://localhost:8000/images/1482833485.jpg",
"pivot": {
"user_id": 4,
"car_id": 177,
"type": "car"
}
},
"both": [
{
"name": "Abdulaziz Alomhsen",
"age": "30",
"created_at": "2017-02-28 09:36:15",
"updated_at": "2017-03-08 08:46:06",
"status": "جايه",
"pic": "http://localhost:8000/images/1488714520.jpg",
"id": 2,
"pivot": {
"user_id": 4,
"driver_id": 2,
"type": "driver"
}
},

Get value of json array based on variable

I have a json that begins like this. I can access the User, Claim, and related first-level data with no problem. I'm trying to access data within ClaimResponse for a specific variable (i.e. where User.id=3). I've tried variations of x.ClaimResponse[Userid].id, with no luck. Any immediate ideas at how I can get at this data?
{
"data": [
{
"User": {
"score": "272",
"id": "3"},
"Claim": {
"id": "2",
"user_id": "3",
"expiration": "2013-12-31 23:59:59",
"editor_content": null,
"point": "56.67",
"claim_count": "3",
"editor_pick": "0",
"isCounterClaimed": "1",
"headline": null,
"subhead": null,
"initial_point": "40",
"claim_maker_cost": "60",
"exp": "Dec 31, 2013"
},
"ClaimResponse": [
{
"id": "4",
"claim_id": "2",
"expiration": null,
"response_type": "nay",
"source": null,
"created": "2013-05-15 03:28:18",
"modified": "2013-05-15 03:28:18"
},
{
"id": "12",
"claim_id": "2",
"expiration": null,
"response_type": "yeh",
"source": null,
"created": "2013-05-15 14:47:30",
"modified": "2013-05-15 14:47:30"
},
{
"id": "13",
"claim_id": "2",
"expiration": null,
"response_type": "nay",
"source": null,
"created": "2013-05-15 15:59:59",
"modified": "2013-05-15 15:59:59"
},
{
"id": "50",
"claim_id": "2",
"expiration": null,
"response_type": "yeh",
"source": null,
"created": "2013-05-24 14:32:23",
"modified": "2013-05-24 14:32:23"
},
{
"id": "71",
"claim_id": "2",
"expiration": null,
"response_type": "yeh",
"source": null,
"created": "2013-05-29 04:37:19",
"modified": "2013-05-29 04:37:19"
}
(...)
[] operator is for getting data based on index from array, e.g if ClaimResponse[0] you get user with id 4
Are you trying to grab a specific ClaimResponse with a particular id? If so, try this:
var arr = x.ClaimResponse,
userId = "3", //Set this to the uid you want
length = arr.length,
claim = null; //This variable stores the correct claim
for (var i = 0; i < length; i++) {
claim = arr[i];
if (claim.id === userId) {
break;
}
}
you can run a for loop for array got after fetching data with key ClaimResponse that returns an array.
Like
public static JSONObject ObjectWithIdandReponse(int _id,JSONArray responseArray) {
for (JSONObject Obj in responseArray)
{
if(Obj.id==_id)
return Obj;
}
}
Make method that returns JSON Obj and pass parameter as user id and array of claims

Resources