How to append key v...
 
Notifications
Clear all

[Solved] How to append key value pairs in javascript object array

2 Posts
1 Users
0 Likes
489 Views
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

How to push/append key-value pairs into a javascript object array?

Issue Tags
1 Reply
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

Check this out: How to push key and value into array in javascript - Stack Overflow

Here is an example:

In the first if, this is what I am getting in the object structure:

this.recordObj ----->

[
   {
      "record":{
         "Id":"a1R9H0000002Z05UAE",
         "Name":"Q-00095",
         "SBQQ__Opportunity2__c":"0069H000001GY1yQAG"
      },
      "isRedColor":false,
      "isYellowColor":false,
      "isGreenColor":true
   }
]

This is what I want to achieve when adding the second if section:

this.recordObj ----->
[
   {
      "record":{
         "Id":"a1R9H0000002Z05UAE",
         "Name":"Q-00095",
         "SBQQ__Opportunity2__c":"0069H000001GY1yQAG"
      },
      "isRedColor":false,
      "isYellowColor":false,
      "isGreenColor":true,
      "isRedColorSpares":false,
      "isYellowColorSpares":false,
      "isGreenColorSpares":true
   }
]

js code:

import { LightningElement, api, track, wire } from "lwc";
import retrieveQuoteData from "@salesforce/apex/getRelatedQuotesDDSummaryLWC.retrieveQuoteData";
import { getRecord } from "lightning/uiRecordApi";
// const OPP_FIELDS = ["Opportunity.Name"];

export default class dDSummary extends LightningElement {
	@api recordId;
	// @track currentOptyName;
	// @track searchOptyName;
	@track records;
	@track dataNotFound;
	@track recordObj = [];

	// handleChangeOptyName(event) {
	// 	this.currentOptyName = event.target.value;
	// }
	// handleOptySearch() {
	// 	this.searchOptyName = this.currentOptyName;
	// }

	// @wire(getRecord, { recordId: "$recordId", fields: OPP_FIELDS })
	// wiredOpp({ error, data }) {
	// 	if (data) {
	// 		this.currentOptyName = data.fields.Name.value;
	// 	} else if (error) {
	// 		console.log("UAC: error " + JSON.stringify(error));
	// 	}
	// }

	// @wire(retrieveQuoteData, { keySearch: "$searchOptyName" })
	// wireRecord({ data, error }) {
	// 	if (data) {
	// 		this.records = data;
	// 		this.error = undefined;
	// 		this.dataNotFound = "";
	// 		if (this.records === "") {
	// 			this.dataNotFound = "No Quotes found for the Opportunity";
	// 		}
	// 		this.error = error;
	// 		this.data = undefined;
	// 	}
	// }

	// @wire(retrieveQuoteData, { keySearch: "$recordId" })
	// wireRecord({ data, error }) {
	// 	if (data) {
	// 		this.records = data;
	// 		this.error = undefined;
	// 		this.dataNotFound = "";
	// 		if (this.records === "") {
	// 			this.dataNotFound = "No Quotes found for the Opportunity";
	// 		}
	// 		this.error = error;
	// 		this.data = undefined;
	// 	}
	// }

	@wire(retrieveQuoteData, { keySearch: "$recordId" })
	wireRecord({ data, error }) {
		var key;
		if (data) {
			this.records = data;
			if (data !== undefined && data !== null) {
				for (key in data) {
					if (data[key].C1DDAPPEMP__c < 0) {
						this.recordObj.push({
							record: data[key],
							isRedColor: true,
							isYellowColor: false,
							isGreenColor: false
						});
						console.log("Checking isRedColor C1DDAPPEMP__c -----> " + data[key].C1DDAPPEMP__c);
					} else if (data[key].C1DDAPPEMP__c >= 0 && data[key].C1DDAPPEMP__c <= 30) {
						this.recordObj.push({
							record: data[key],
							isRedColor: false,
							isYellowColor: true,
							isGreenColor: false
						});
						console.log("Checking isYellowColor C1DDAPPEMP__c -----> " + data[key].C1DDAPPEMP__c);
					} else if (data[key].C1DDAPPEMP__c > 30) {
						this.recordObj.push({
							record: data[key],
							isRedColor: false,
							isYellowColor: false,
							isGreenColor: true
						});
						console.log("Checking isGreenColor C1DDAPPEMP__c -----> " + data[key].C1DDAPPEMP__c);
						console.log("Checking isGreenColor this.recordObj -----> " + JSON.stringify(this.recordObj));
						console.log("Checking isGreenColor this.records -----> " + JSON.stringify(this.records));
					}
				}

				for (key in this.recordObj) {
					if (this.recordObj[key].record.SDDAPEMP__c < 0) {
						this.recordObj[key].isRedColorSpares = true;
						this.recordObj[key].isYellowColorSpares = false;
						this.recordObj[key].isGreenColorSpares = false;
					} else if (this.recordObj[key].record.SDDAPEMP__c >= 0 && this.recordObj[key].record.SDDAPEMP__c <= 30) {
						this.recordObj[key].isRedColorSpares = false;
						this.recordObj[key].isYellowColorSpares = true;
						this.recordObj[key].isGreenColorSpares = false;
					} else if (this.recordObj[key].record.SDDAPEMP__c > 30) {
						this.recordObj[key].isRedColorSpares = false;
						this.recordObj[key].isYellowColorSpares = false;
						this.recordObj[key].isGreenColorSpares = true;
					}
				}
				console.log("Checking isYellowColorSpares SDDAPEMP__c -----> " + data[key].SDDAPEMP__c);
				console.log("Checking isYellowColorSpares this.recordObj -----> " + JSON.stringify(this.recordObj));
				console.log("Checking isYellowColorSpares this.records -----> " + JSON.stringify(this.records));
			}
			this.error = undefined;
			this.dataNotFound = "";
			if (this.records === "") {
				this.dataNotFound = "No Quotes found for this Opportunity";
			}
			this.error = error;
			this.data = undefined;
		}
	}
}
Reply
Share: