目前我的物件看起來像這樣
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
我想把它轉換成這樣的一個元素的陣列
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)
我用 Object.values 嘗試過(我的 JSON 物件需要轉換為單元素陣列),但它使它成為一個包含多個元素的陣列
uj5u.com熱心網友回復:
你只需要將你的物件推入一個空白陣列
var objArray = [];
objArray.push(YourJavascriptObject);
uj5u.com熱心網友回復:
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]"}]
uj5u.com熱心網友回復:
您可以使用以下代碼來解決您的問題。
let arr = [ obj ];
注意:替換obj為您的物件,以及arr您的陣列變數名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/446379.html
標籤:javascript 反应 json javascript 对象
上一篇:如何使用自定義名稱的Codable協議決議嵌套的JSON資料
下一篇:表單如何只接受特定的資料型別?
