currently my object look like this
Object application: null area: null attributeSetID: 1043 attributeSetName: "Air Valve_Pneumatics & Hydraulics" attributeSetValues: "{\"Brand\": \"Janatics Pnuematic\", \"Color\": \"\", \"Medium\": \"Dry Compressed Air, Lubricated Compressed Air\", \"Status\": \"Active\", \"Stroke\": \"\", \"Torque\": \"\", \"Weight\": \"\", \"Orifice\": \"\", \"Synonym\": \"\", \"Material\": \"Aluminium, Nitrile, Brass, Plastic\", \"Standard\": \"ISO 9001:2008\", \"Dimension\": \"Datasheet\", \"Flow Rate\": \"100, 400, 525\", \"Item Code\": \"M5, 1/8, 1/4\", \"Port Size\": \"5, 3.175, 6.35\", \"Stroke UOM\": \"\", \"Torque UOM\": \"\", \"Weight UOM\": \"\", \"Orifice UOM\": \"\", \"Dimension UOM\": \"mm\", \"Exhaust Noise\": \"\", \"Flow Rate UOM\": \"l/m\", \"Port Size UOM\": \"mm\", \"Working Pressure\": \"1.5 - 10\", \"Exhaust Noise UOM\": \"\", \"Temperature Range\": \"5 - 60\", \"Working Pressure UOM\": \"bar\", \"Temperature Range UOM\": \"°C\"}" attributes: ['{"Brand": "Janatics Pnuematic", "Color": "", "Medi…ssure UOM": "bar", "Temperature Range UOM": "°C"}'] availability: "In Stock" badges: ['new'] branchLocation: null branchLocationForSupplierView: null brand: {name: null, slug: null, image: null} brandName: "Janatics Pnuematic" categories: [] category: "" childCategory: "Pneumatics & Hydraulics" compareAtPrice: 0 country: null customfields: {} dataSheets: null errorImage: "https://yellow-pages-bahrain.s3.me-south-1.amazonaws.com/SPA_Images/productImages/Default Images/No Image Products/air-valve.webp" id: 13267 images: ['https://yellow-pages-bahrain.s3.me-south-1.amazona…ges/Al Zerwa Trading LLC/Air Valve/air-valve.webp'] isHeadOffice: false isProductNeedsToBeShown: true itemAttributeValues: "{\"Brand\": \"Janatics Pnuematic\", \"Color\": \"\", \"Medium\": \"Dry Compressed Air, Lubricated Compressed Air\", \"Status\": \"Active\", \"Stroke\": \"\", \"Torque\": \"\", \"Weight\": \"\", \"Orifice\": \"\", \"Synonym\": \"\", \"Material\": \"Aluminium, Nitrile, Brass, Plastic\", \"Standard\": \"ISO 9001:2008\", \"Dimension\": \"Datasheet\", \"Flow Rate\": \"100, 400, 525\", \"Item Code\": \"M5, 1/8, 1/4\", \"Port Size\": \"5, 3.175, 6.35\", \"Stroke UOM\": \"\", \"Torque UOM\": \"\", \"Weight UOM\": \"\", \"Orifice UOM\": \"\", \"Dimension UOM\": \"mm\", \"Exhaust Noise\": \"\", \"Flow Rate UOM\": \"l/m\", \"Port Size UOM\": \"mm\", \"Working Pressure\": \"1.5 - 10\", \"Exhaust Noise UOM\": \"\", \"Temperature Range\": \"5 - 60\", \"Working Pressure UOM\": \"bar\", \"Temperature Range UOM\": \"°C\"}" location: "Dubai" name: "And Valve" noOfProducts: 0 parentCategory: "Machine Parts" placeOfOrigin: "India" price: 0 productClass: "Valve" productDescription: null productFeature: "AND valve delivers air at the outlet (A) only when air signal is present at both inputs (X and Y). If two signals of different pressure is applied, the lower pressure is connected to outlet (A)." productManufacturer: "Janatics Pnuematic" productName: "Air Valve" rating: "4" revenue: 0 reviews: "12" rootCategory: "Industrial Equipment & Machinery" searchValueInAttribute: null shortDescription: "Ampere, Voltage" showInFilter: "Material,Color,Port Size,Dimension,Temperature Range" showInMultiple: null showInProduct: "Material,Color,Port Size,Dimension,Temperature Range,Flow Rate,Working Pressure" showInRanges: null showUOM: "Port Size,Dimension,Temperature Range,Flow Rate,Working Pressure,Weight,Orifice,Torque,Stroke,Exhaust Noise" sku: null slug: "And Valve" state: null subClass: "Air" supplier: "Al Zerwa Trading LLC" supplierID: 6 supplierStatus: "Distributors" totalCountOfProductsBySupplier: 0 totalRevenue: 0 [[Prototype]]: Object
i want to convert it to an array of one element like this
Array(1) 0: application: null area: null attributeSetID: 1095 attributeSetName: "Back Saw_Cutting Tools" attributeSetValues: "{\"Brand\": \"Kistenmacher\", \"Length\": \"250, 300, 350\", \"Status\": \"Active\", \"Synonym\": \"\", \"Item Code\": \"111 - 250, 111 - 300, 111 - 350\", \"Length UOM\": \"mm\", \"Tooth Pitch\": \"12\", \"Handle Material\": \"Wood\", \"Tooth Pitch UOM\": \"tpi\"}" attributes: ['{"Brand": "Kistenmacher", "Length": "250, 300, 350…ndle Material": "Wood", "Tooth Pitch UOM": "tpi"}'] availability: "In Stock" badges: ['new'] branchLocation: null branchLocationForSupplierView: null brand: {name: null, slug: null, image: null} brandName: "Kistenmacher" categories: [] category: "" childCategory: "Cutting Tools" compareAtPrice: 0 country: null customfields: {} dataSheets: null errorImage: "https://yellow-pages-bahrain.s3.me-south-1.amazonaws.com/SPA_Images/productImages/Default Images/No Image Products/back-saw.webp" id: 13547 images: ['https://yellow-pages-bahrain.s3.me-south-1.amazona…mages/Al Zerwa Trading LLC/Back Saw/back-saw.webp'] isHeadOffice: false isProductNeedsToBeShown: true itemAttributeValues: "{\"Brand\": \"Kistenmacher\", \"Length\": \"250, 300, 350\", \"Status\": \"Active\", \"Synonym\": \"\", \"Item Code\": \"111 - 250, 111 - 300, 111 - 350\", \"Length UOM\": \"mm\", \"Tooth Pitch\": \"12\", \"Handle Material\": \"Wood\", \"Tooth Pitch UOM\": \"tpi\"}" location: "Dubai" name: "Back Saws" noOfProducts: 0 parentCategory: "Hand Tools" placeOfOrigin: null price: 0 productClass: "Saw" productDescription: null productFeature: "With closed wooden handle, lacquered, rivetted" productManufacturer: "E.G. Kistenmacher" productName: "Back Saw" rating: "4" revenue: 0 reviews: "12" rootCategory: "Tools & Hardware" searchValueInAttribute: null shortDescription: null showInFilter: "Handle Material,Length,Tooth Pitch,Item Code,Brand" showInMultiple: null showInProduct: "Handle Material,Length,Tooth Pitch,Item Code,Brand,Synonym,Status" showInRanges: null showUOM: "Length,Tooth Pitch" sku: null slug: "Back Saws" state: null subClass: "Back" supplier: "Al Zerwa Trading LLC" supplierID: 6 supplierStatus: "Distributors" totalCountOfProductsBySupplier: 0 totalRevenue: 0 [[Prototype]]: Object length: 1 [[Prototype]]: Array(0)
i tried it with Object.values(my JSON object need to convert to single element array) but its making it an array of multiple elements
CodePudding user response:
you just need to push your object into a blank array
var objArray = [];
objArray.push(YourJavascriptObject);
CodePudding user response:
let arr1 = [];
let obj ={"\\name\\":"\\XYZ\\","\\email\\":"\\[email protected]\\"};
let Objstr = JSON.stringify(obj).replace(/\\/g, '');
const newArray = [...arr1, Objstr ];
console.log(newArray);//[{name:"XYZ",email:"[email protected]"}]
CodePudding user response:
You can use the following code to solve your issue.
let arr = [ obj ];
Note: Replace obj
with your object, and arr
with your array variable name.