Оптимизируй код js без использования библиотек: function searchInJsonPath(json,…
- User
Оптимизируй код js без использования библиотек: function searchInJsonPath(json, searchPattern) { let results = { searchPattern: searchPattern, count: 0, paths: [], values: [] }; function search(jsonObj, path) { for (let key in jsonObj) { let newPath = path + '.' + key; if (jsonObj.hasOwnProperty(key)) { if (typeof jsonObj[key] === 'object') { search(jsonObj[key], newPath); } else { if (key === searchPattern) { results.count++; results.paths.push(newPath); results.values.push(jsonObj[key]); } } } } } search(json, ''); return results; }; function getShortObjFrom_searchInJsonPath(json,shortJsonPath){ let result = {} function getMatchCount(arg){ return ( Number(( searchInJsonPath(json, arg) )['count']) > 0 ) ? Number(( searchInJsonPath(json, arg) )['count']) : 0 } let matchLength = getMatchCount(shortJsonPath); if(matchLength){ for(i=0;i 0) ? result : undefined }; function Object_keys(obj){ return (Object.keys(obj).length > 0) ? Object.keys(obj) : undefined } function processJsonPaths(jsonPathList, json) { var result = []; for (var i = 0; i < jsonPathList.length; i++) { var path = jsonPathList[i].split('.'); var value = json; for (var j = 0; j < path.length; j++) { if (path[j] === '') continue; if (value.hasOwnProperty(path[j])) { value = value[path[j]]; } else { value = null; break; } } result.push(value); } return result; } function getAllValuesFromShortJsonPats(json, jsonShortPath){ let result; result = processJsonPaths(Object_keys(getShortObjFrom_searchInJsonPath(json, jsonShortPath)), json); return (Object.keys((result)).length > 0) ? result : undefined } let json = require('./input.json'); let pattern = '_id' console.log(getAllValuesFromShortJsonPats(json,pattern)) console.log((getAllValuesFromShortJsonPats(json,pattern)).length)
- Assistant
function searchInJsonPath(json, searchPattern) { let results = { searchPattern: searchPattern, count: 0, paths: [], values: [] };
function search(jsonObj, path) { for (let key in jsonObj) { let newPath = path + '.' + key; if (jsonObj.hasOwnProperty(key)) { if (typeof jsonObj[key] === 'object') { search(jsonObj[key], newPath); } else { if (key === searchPattern) { results.count++; results.paths.push(newPath); results.values.push(jsonObj[key]); } } } } }
- User
напиши функцию на js чтобы поменять в объекте ключ и значение ключа
- Assistant
Вот пример функции, которая меняет местами ключ и значение ключа объекта:
function swapKeyValue(obj) { const swappedObj = {}; for (let key in obj) { if (obj.hasOwnProperty(key) && typeof obj[key] !== 'object') { swappedObj[obj[key]] = key; } else { swappedObj[key] = swapKeyValue(obj[key]); // рекурсивно вызываем swapKeyValue для вложенных объектов } } return swappedObj; }Пример использования:
const obj = { key1: 'value1', key2: 'value2', key3: { subKey1: 'subValue1', subKey2: 'subValue2' } }; const swappedObj = swapKeyValue(obj); console.log(swappedObj); // Output: { // value1: 'key1', // value2: 'key2', // key3: { // subValue1: 'subKey1', // subValue2: 'subKey2' // } // } - User
напиши функцию на js чтобы поменять в объекте все ключи и значения местами
- Assistant
Вот пример функции, которая меняет местами все ключи и значения объекта:
function swapKeysAndValues(obj) { const swappedObj = {}; for (let key in obj) { if (obj.hasOwnProperty(key)) { if (typeof obj[key] === 'object') { // для вложенных объектов вызываем рекурсивно swapKeysAndValues swappedObj[obj[key]] = swapKeysAndValues(key); } else { swappedObj[obj[key]] = key; // меняем местами ключ и значение } } } return swappedObj; }Пример использования:
const obj = { key1: 'value1', key2: 'value2', key3: { subKey1: 'subValue1', subKey2: 'subValue2' } }; const swappedObj = swapKeysAndValues(obj); console.log(swappedObj); // Output: { // value1: 'key1', // value2: 'key2', // subValue1: 'subKey1', // subValue2: 'subKey2' // }