How to use the isPlainObject function from lodash

Find comprehensive JavaScript lodash.isPlainObject code examples handpicked from public code repositorys.

lodash.isPlainObject is a method that checks if the passed object is a plain object or not.

3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
* }
*
* _.isPlainObject(new Shape);
* // => false
*
* _.isPlainObject([1, 2, 3]);
* // => false
*
* _.isPlainObject({ 'x': 0, 'y': 0 });
* // => true
fork icon73
star icon711
watch icon29

+ 5 other calls in file

211
212
213
214
215
216
217
218
219
220
module.exports.isNumber            = _.isNumber;
module.exports.isNumeric           = _.isNumeric;
module.exports.isObject            = _.isObject;
module.exports.isObjectLike        = _.isObjectLike;
module.exports.isOdd               = _.isOdd;
module.exports.isPlainObject       = _.isPlainObject;
module.exports.isPositive          = _.isPositive;
module.exports.isRegExp            = _.isRegExp;
module.exports.isSafeInteger       = _.isSafeInteger;
module.exports.isSequential        = _.isSequential;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.isPlainObject work?

lodash.isPlainObject is a function in the Lodash library that checks if a given value is a plain object, meaning it was created with the object literal syntax {} or with the Object constructor and does not inherit from a prototype other than Object.prototype. The function returns true if the value is a plain object and false otherwise. It also returns false for null and non-object values. The function performs this check by verifying that the value is an object and that its prototype is either null or Object.prototype.

12
13
14
15
16
17
18
19
20
21

// FIXME: We don't want empty objects to be added to the resulting JSON.
// This is the default behaviour of Bookshelf when using single entities,
// but for collections it isn't. Looks like a bug in their side (see
// https://github.com/tgriesser/bookshelf/issues/753)
const isEmptyPlainObject = (value) => _.isPlainObject(value) && _.isEmpty(value);

// FIXME: This is a workaround because Swagger doesn't allow nullable
// fields. Check https://github.com/OAI/OpenAPI-Specification/issues/229.
const isNullOrEmptyPlainObject = (value) => (value === null) || isEmptyPlainObject(value);
fork icon10
star icon0
watch icon2

+ 3 other calls in file

363
364
365
366
367
368
369
370
371
372
/**
 * @param value
 * @return {Boolean}
 */
static isObject(value) {
    return _.isPlainObject(value);
};

/**
 * @param value
fork icon5
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
const _ = require("lodash");

const obj = { a: 1, b: 2 };
const arr = [1, 2, 3];
const func = () => {};

console.log(_.isPlainObject(obj)); // true
console.log(_.isPlainObject(arr)); // false
console.log(_.isPlainObject(func)); // false

In this example, we import the lodash library and create an object, array, and function. We then use the _.isPlainObject() method to check whether each of these values is a plain object. As expected, obj returns true, while arr and func return false.

470
471
472
473
474
475
476
477
478
479
480
481
482
console.log(isObject); // => true


const isObjectLike = _.isObjectLike({});
console.log(isObjectLike); // => true


const isPlainObject = _.isPlainObject({});
console.log(isPlainObject); // => true


const isRegExp = _.isRegExp(/abc/);
console.log(isRegExp); // => true
fork icon0
star icon4
watch icon0

+ 15 other calls in file

586
587
588
589
590
591
592
593
594
595
  sanitizedValue = value;
  break;
case "focusMode":
  hasCorrectType =
    value &&
    _.isPlainObject(value) &&
    value.isOn &&
    _.isBoolean(value.isOn) &&
    _.isString(value.buttonLabel) &&
    (value.buttonAnchor === null || _.isString(value.buttonAnchor)) &&
fork icon0
star icon3
watch icon1

285
286
287
288
289
290
291
292
293
294
295
}


// Write file
extras.write = function (file, content) {
  file = extras.resolve(file)
  if (Array.isArray(content) || _.isPlainObject(content)) {
    if (file.endsWith('.json')) {
      content = JSON.stringify(content)
    } else if (file.endsWith('.yml')) {
      content = yaml.dump(content)
fork icon0
star icon3
watch icon1

+ 6 other calls in file

318
319
320
321
322
323
324
325
326
327
}
function isRecord(arg) {
    return (lodash.isPlainObject(arg) || helpers.isClassInstance(arg) || arg instanceof typend.Collection);
}
function isPlainRecord(arg) {
    return lodash.isPlainObject(arg) || arg instanceof typend.Collection;
}
function hasPostConstruct(target) {
    return (target != null &&
        Reflect.hasMetadata(inversifyAsync.METADATA_KEY.POST_CONSTRUCT, target.constructor));
fork icon0
star icon2
watch icon0

+ 14 other calls in file

42
43
44
45
46
47
48
49
50
51
52
}


function isValidRewriteConfig(rewriteConfig) {
  if (_.isFunction(rewriteConfig)) {
    return true
  } else if (!_.isEmpty(rewriteConfig) && _.isPlainObject(rewriteConfig)) {
    return true
  } else if (
    _.isUndefined(rewriteConfig) ||
    _.isNull(rewriteConfig) ||
fork icon0
star icon1
watch icon1

106
107
108
109
110
111
112
113
114
115
// if (process.env.NODE_ENV !== __constants.CUSTOM_CONSTANT.PROD_ENV && err) {
resData.error = err
// }
if (!response.is_sent) {
  response.is_sent = true
  if (!_.isEmpty(options.headers) && !_.isArray(options.headers) && _.isPlainObject(options.headers)) {
    response.set(options.headers)
  }
  response.status(validCodeObj.statusCode || 200).json(resData)
}
fork icon0
star icon0
watch icon1

78
79
80
81
82
83
84
85
86
87
88
 * @param  {Object}  context [description]
 * @param  {*}       opts    [description]
 * @return {Boolean}         [description]
 */
function isContextless(context, opts) {
  return _.isPlainObject(context) && _.isEmpty(opts)
}


function mapLegacyProxyHostOption(options) {
  // set options.headers.host when option.proxyHost is provided
fork icon0
star icon0
watch icon1

301
302
303
304
305
306
307
308
309
310
 * @param {*} values object
 * @returns {result: {rows, fields}}
 */
const add = async (conn, tableName, values) => {
    // check values is not empty
    if (!(values && _.isPlainObject(values) && !_.isEmpty(values))) {
        return {
            error: 'values is required'
        }
    }
fork icon0
star icon0
watch icon1

+ 20 other calls in file

119
120
121
122
123
124
125
126
127
128
  json: (arg) => JSON.stringify(arg, null, 2),
  yaml: (arg) => yaml.dump(arg),
  none: (arg) => arg
};
function logger(index, defaultColorOrOptions, defaultSerializeAsOrAddAlways) {
  const defaultOptions = _.isPlainObject(defaultColorOrOptions) ? defaultColorOrOptions : {
    color: defaultColorOrOptions ?? "gray",
    serializeAs: defaultSerializeAsOrAddAlways ?? "yaml"
  };
  const addAlways = _.isBoolean(defaultSerializeAsOrAddAlways) ? defaultSerializeAsOrAddAlways : true;
fork icon0
star icon0
watch icon0

+ 7 other calls in file

644
645
646
647
648
649
650
651
652
653
  return _.union(objValue, srcValue);
}

if (['where', 'having'].includes(key)) {
  return combineWheresWithAnd(objValue, srcValue);
} else if (key === 'attributes' && _.isPlainObject(objValue) && _.isPlainObject(srcValue)) {
  return _.assignWith(objValue, srcValue, (objValue, srcValue) => {
    if (Array.isArray(objValue) && Array.isArray(srcValue)) {
      return _.union(objValue, srcValue);
    }
fork icon0
star icon0
watch icon0

+ 7 other calls in file

261
262
263
264
265
266
267
268
269
270
                    targetId: target.toString(),
                    typeName: "GenericLink",
                };
        }
    }
} else if (_.isObject(edgeSpecs) || _.isPlainObject(edgeSpecs)) {
    if (edgeSpecs.sourceId && edgeSpecs.targetId && Utils.isStringOrNumber(edgeSpecs.sourceId) && Utils.isStringOrNumber(edgeSpecs.targetId)) {
        if (returnArray) {
            return [edgeSpecs.sourceId.toString(), edgeSpecs.targetId.toString()];
        } else {
fork icon0
star icon0
watch icon2

139
140
141
142
143
144
145
146
147
148
this.client = {}; // 阿里云OSS客户端
this.finalPrefix = ''; // 最终计算出来的prefix路径
this.currentProvider = {}; // 当前提供服务商信息
// 合并配置信息
this.config = lodash.mergeWith(lodash.cloneDeep(this.config), config || {}, function (objVal, srcVal) {
    if (lodash.isPlainObject(objVal) && lodash.isPlainObject(srcVal)) {
        return lodash.merge(objVal, srcVal);
    }
    return srcVal;
});
fork icon0
star icon0
watch icon1

+ 3 other calls in file

210
211
212
213
214
215
216
217
218
219
super();

if (arguments.length === 1 && _.isPlainObject(database)) {
  // new Sequelize({ ... options })
  options = database;
} else if (arguments.length === 1 && typeof database === 'string' || arguments.length === 2 && _.isPlainObject(username)) {
  // new Sequelize(URI, { ... options })
  options = username ? { ...username } : Object.create(null);

  _.defaultsDeep(options, parseConnectionString(arguments[0]));
fork icon0
star icon0
watch icon420

+ 26 other calls in file

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)